Long Polling: A easily implemented concept

Image Source: Pub/Nub.com

How does HTTP Long Polling proceed?

  1. Clients use regular HTTP to make a request and wait for a response.
  2. The server holds the response if there is no data update or has a timeout.
  3. When there is an update, the server sends a complete response to the client.
  4. The client resends the long poll request either immediately or after an acceptable latency period.
  5. Each long-pull request has a timeout, and a client has to periodically reconnect again.

AJAX Polling

Most of the AJAX apps use Polling as a standard technique where the idea is that the client needs to poll a server repeatedly. The client makes a request and waits for the response and if there is no data available, the server returns an empty response — unlike the traditional HTTP Long Polling. The process can be repeated multiple times — as much as we need it.

Web Sockets

Web socket is a network-communication protocol that was specially designed to work over Transmission Control Protocol (TCP). The sockets open in a two-way channel — client and server. This creates a hotline between them leading to two endpoints.

Server-Sent Events (SSEs)

The next stop is Sever Sent Events (SSEs), a connection where a client establishes a steady and long-term connection with the server. Then, the server uses the connection to send the data. Please note, that the client can’t send data to the server and would require a separate technology if required. A client just uses regular HTTP to make requests and wait for a response.

Advantages of Long Polling

  1. Easy Implementation
  2. Universal Support
  3. The browser doesn’t have to send queries repeatedly to find the status of its requests.

Disadvantages of Long Polling

  1. This is one-directional. The data transfer from the client to the server can happen via normal HTTP requests only.
  2. The server has to bear an excessive load.
  3. There is a probable race condition of multiple requests from the same client receiving messages out of order (two tabs open)

Conclusion

Long Polling is a good idea when a simple implementation for regularly updating clients with new information like updating a dashboard every minute with new data. There might be challenges like when the client needs to use a medium other than normal HTTP web request, there are Web Sockets on the rescue.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store