HTTP Short vs Long Polling vs WebSockets vs SSE

Aditi
TechieAhead
Published in
3 min readFeb 7, 2023

Web applications were originally designed as a simple client-server model where the web client initiates an HTTP request requesting some data from the server. For example, a basic web application with the client-server model flow will be as follows :

  1. A client makes an HTTP request requesting a web page from a server.
  2. The server calculates the response.
  3. The server sends the response to the client.

As developers began to explore ways to implement more “real-time” applications. The HTTP protocol made these sorts of use cases very challenging. As a result, creative ways to manipulate HTTP request-response model into a more real-time model began to emerge.

HTTP Short Polling

In HTTP short polling, the client keeps making the HTTP requests at a regular interval for example every 1–2 seconds. In this case, most of the requests might get an empty response because the server might not have any updates which are useful for the client.

Disadvantages

  • High HTTP Overhead
  • Unnecessary network calls. Lots of requests going back and forth.
  • Not suitable for real-time communication applications.

HTTP Long Polling

The client can make a request to the server for the data using the HTTP request. But, here is the catch, the client waits for the server to provide the response. There will be a connection opened till the server has a response to send back or a timeout occurs.

Advantages

  • Low HTTP Overhead

Disadvantages

  • It is inefficient for designing chat applications. If a user does not chat much, long polling still makes periodic connections after timeouts.

WEBSOCKETS

From the official documentation:

A WebSocket is a persistent connection between a client and a server. WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection. At its core, the WebSocket protocol facilitates message passing between a client and server.

Hence, WebSocket is the most common solution for sending asynchronous updates from server to client. The client initiates a WebSocket connection. It is bi-directional and persistent. Through this constant connection, a server could send updates to a client.

Advantages

  • Realtime data transfer
  • Low communication overhead as we are doing the handshaking only once at the beginning.

Applications

  • Stock market ticker
  • Live experiences
  • Multi-player games
  • Chat

Server Side Events

According to the definition:

SSE is a server-push technology enabling a client to receive automatic updates from a server. Using SSE, the clients make a persistent long-term connection with the server. Then, the server uses this connection to send the data to the client. It is important to note that the client can’t send the data to the server using the SSE.

Hence unlike WebSockets, Server-Sent Events are a one-way communication channel where events flow from server to client only.

Applications

  • Deployment logs streaming like in Jenkins
  • Breaking news, Sports Score updates
  • Real-time notifications
  • Instagram live interactions
  • Stock market updates

That all folks here! If you liked this article, please don’t forget to click 👏👏👏 and share. Stay tuned for the next post!

Also, to be notified about new articles and stories, do follow us on Medium, Instagram, Twitter, Pinterest and LinkedIn. Cheers!

--

--

Aditi
TechieAhead

Staff Engineer | Software Evangelist | Loves to spread knowledge and write articles https://twitter.com/AheadTechie