Learn websocket basics - Comparison with HTTP protocol

Web socket beginner tutorials

WebSocket is a protocol with two way HTTP connection between client and server. Communication happened over a single socket layer,

Traditional client-server communication web applications

In normal Web applications, the client initiates the request, and the server serves the response. Here communication is over HTTP protocol, Once the content is served on the page, no updated content will be served until the page is refreshed.

Think of the following use cases where real-time communication is required in Facebook Chat, Live Cricket scores, Stock Quotes News feed Online games Real-time applications communications. For example, Now think of live Cricket based score websites/ Facebook chat sites, the data displayed on live cricket scores is very frequent.

The live score once displayed might be outdated. Continued refresh is required to get the newly updated score. It is a poor user experience For real-time web app communication, Traditional client-server communication is not used as this has a poor user experience.

Solutions to this problem are having two options

  1. Polling
  2. Long Polling

Polling- alternative to WebSocket

1. The client sends a request to the server, the HTTP connection is opened
2. The server serves the response over an HTTP connection and the server closes this connection
3. step1 and step2 happen at frequent intervals for getting updated content at frequent intervals for every request, the HTTP connection is created Once data is served, the connection will be closed.

Polling disadvantages

1. More bandwidth is required if there is no content update
2. Connection open/close is an expensive operation for every request during interval time.

Long polling alternative to WebSocket

1. This method is asynchronous polling
2. The server keeps the connection open until the configured period timeout is reached.

Advantages

When data is huge, it becomes polling.

Http

Websockets

Oneway communication - The client sends a request to the server, and the server sends the responses.

Two-way communication ie duplex channel

The server can not push to the client.

Core feature and Server push are possible.

Cache inbuilt support

The cache is not possible.

All clients and browsers support there.

Only Modern browsers support

All technologies support is available.

Only Modern languages support is available.

WebSocket support in all popular browsers. The following libraries have support for implementing WebSocket implementation.

socksJS

Socket.io