Ajax Polling vs Long-Polling vs WebSockets vs Server-Sent Events

Ajax Polling, Long-Polling, WebSockets, and Server-Sent Events are popular communication protocols between clients like web browsers and web servers.


First, let’s start with understanding what a standard HTTP web request looks like. Following are a sequence of events for a regular HTTP request:

  1. The client opens a connection and requests data from the server.

  2. The server calculates the response.

  3. The server sends the response back to the client on the opened request.


Ajax Polling

Polling is a standard technique used by the vast majority of AJAX applications. The basic idea is that the client repeatedly polls (or requests) a server for data. The client makes a request and waits for the server to respond with data. If no data is available, an empty response is returned.

  1. The client opens a connection and requests data from the server using regular HTTP.

  2. The requested webpage sends requests to the server at regular intervals (e.g., 0.5 seconds).

  3. The server calculates the response and sends it back, just like regular HTTP traffic.

  4. The client repeats the above three steps periodically to get updates from the server

The problem with Polling is that the client has to keep asking the server for any new data. As a result, a lot of responses are empty, creating HTTP overhead.


HTTP Long-Polling

This variation of the traditional polling technique allows the server to push information to a client whenever the data is available. With Long-Polling, the client requests information from the server exactly as in regular polling, but with the expectation that the server may not respond immediately. That’s why this technique is sometimes referred to as a “Hanging GET.”

  • If the server does not have any data available for the client, instead of sending an empty response, the server holds the request and waits until some data becomes available.

  • Once the data becomes available, a complete response is sent to the client. The client then immediately re-requests information from the server so that the server will almost always have an available