WebSockets vs Pooling - alexanderteplov/computer-science GitHub Wiki

WebSockets vs Pooling

WebSockets

WebSocket is a computer communication protocol that provides full-duplex communication channels over a single TCP connection.

  • It is different from HTTP but compatible with HTTP.
  • Located at layer 7 in the OSI model and depends on TCP at layer 4.
  • Works over ports 80 and 443 ( in case of TLS encrypted) and supports HTTP proxies and intermediaries.
  • To achieve compatibility, the WebSocket handshake uses an Upgrade header to update the protocol to the WebSocket protocol.

The WebSocket protocol enables interaction between a client and a web server with lesser overheads, providing real-time data transfer from and to the server. WebSockets keeps the connection open, allowing messages to be passed back and forth between the client and the server. In this way, a two-way ongoing conversation can take place between the client and the server.

Ajax Polling

In Ajax polling, a client makes XHR(XMLHttpRequest)/Ajax requests to the server repeatedly at some regular interval to check for new data. A flow for Ajax polling will as follow.

  1. A client initiates requests at small regular intervals (e.g 0.5 Seconds)
  2. The server prepares the response and sends it back to the client just like normal HTTP requests.

Making repeated requests to the server wastes resources as each new incoming connection must be established, the HTTP headers must be passed, a query for new data must be performed, and a response (usually with no new data to offer) must be generated and delivered. The connection must be closed and any resources cleaned up.

Long Polling

As in regular polling, rather than having to repeat this process multiple times for every client until new data for a given client becomes available, Long polling is a technique where the server elects to hold a client connection open for as long as possible, delivering a response only after data becomes available or timeout threshold has been reached. After receiving a response client immediately sends the next request. On the client-side, only a single request to the server needs to be managed. When the response is received, the client can initiate a new request, repeating this process as many times as necessary.

A flow for Long polling will look as follows

  1. A client initiates an XHR/AJAX request, requesting some data from a server.
  2. The server does not immediately respond with request information but waits until there is new information available.
  3. When there is new information available, the server responds with new information.
  4. The client receives the new information and immediately sends another request to the server restarting the process.

Some challenges in long polling

  • Message ordering and delivery guarantees. Message ordering cannot be guaranteed if the same client opens multiple connections to the server. If the client was not able to receive the message then there will be possible message loss.
  • Performance and scaling
  • Device support and fallbacks

Links

⚠️ **GitHub.com Fallback** ⚠️