Polling Vs Long Polling Vs SSE and WebSockets

Arun Chalise
2 min readJul 14, 2021

--

When displaying a continuous stream of data to the client, several options are available:

  1. Short Polling
  2. Long Polling
  3. WebSockets
  4. Server-Sent Events (SSE)

Short Polling

In short polling, the client periodically sends requests to the server at short intervals to check for new data. The server responds with the available data or sends an empty response if no data is ready. This method introduces significant overhead due to repeatedly establishing connections, populating headers, and handling security measures, only to discover that no new data is available.

Long Polling

Long polling improves upon short polling by allowing the server to hold the client’s request open until data becomes available or the request times out. Once the client receives a response, it immediately sends a new request. While this reduces the overhead compared to short polling, there is still bandwidth consumption from continuously re-establishing connections and managing headers. Long polling can also introduce scaling and performance issues, particularly with message ordering. In such cases, additional logic is required to track the last event sent when the client reconnects.

WebSockets

WebSockets establish a full-duplex communication channel between the client and server through an HTTP upgrade request. This allows for real-time, bi-directional communication without the need to repeatedly establish connections. However, the unlimited number of potential connections can overwhelm the server unless mechanisms are in place to limit this. Since WebSockets use a different protocol, they may require additional configuration for firewalls and proxies, which might conflict with enterprise security policies that only permit HTTP traffic. WebSockets are ideal for applications requiring bi-directional communication, such as chat and gaming platforms.

Server-Sent Events (SSE)

SSE is a native HTTP feature that maintains a long-lived connection between the server and client, with the server pushing updates via a special MIME type (text/event-stream). SSE messages follow a predefined format, enabling an efficient publish-subscribe model. With SSE, common challenges like connection drop detection and resuming communication from the last event after reconnection are automatically handled. When using HTTP/2, SSE benefits from automatic multiplexing, which is not available with WebSockets.

For most typical web applications, where only unidirectional data flow from the server is required, SSE is an ideal choice. It simplifies the development process by handling much of the complexity involved in creating a reliable and scalable publish-subscribe model over traditional HTTP.

--

--

No responses yet