DEV Community

Shameel Uddin
Shameel Uddin

Posted on

Understanding Short Polling, Long Polling, Server Sent Events and Web Sockets

Table of Contents

  1. Introduction
  2. Short Polling
  3. Long Polling
  4. SSE
  5. Web Sockets
  6. Conclusion

Introduction

In the dynamic world of web applications, real-time communication has become a fundamental requirement. Whether it's keeping users updated with live data, facilitating instant messaging, or enabling collaborative features, various methods are employed to achieve real-time communication between clients and servers.

This article delves into four distinct approaches:

  1. Short Polling
  2. Long Polling
  3. Server Sent Events
  4. Web Sockets

Understanding these techniques is essential for building responsive and interactive web applications. Let's explore each of them in detail.

Short Polling

  • In short polling, the client repeatedly sends requests to the server at regular intervals to check for updates or new data.

  • The server responds to these requests immediately, even if there is no new information available. This process continues at regular intervals, with the client periodically polling the server for updates.

Short Polling

Long Polling

  • In long polling, the client sends a request to the server, and the server holds onto the request, waiting for updates or new data.

  • It doesn't respond immediately if there is no new information available. The server responds as soon as there is new data to send or after a certain timeout period, and then the client immediately sends another request, creating a cycle of long-polling requests to maintain real-time communication.

Long Polling

SSE

  • In Server-Sent Events (SSE), the client establishes a persistent unidirectional connection with the server using a standard HTTP request.

  • The server sends events or updates to the client as they occur.

  • The connection remains open, and the server can keep sending events to the client without the need for the client to initiate additional requests. This allows for real-time communication from the server to the client.

Server-Sent Events

Web Sockets

  • WebSocket is a communication protocol that enables full-duplex, bidirectional communication over a single, long-lived TCP connection.

  • It's designed to provide real-time, low-latency data exchange between clients (typically web browsers) and servers.

  • Client (usually browser) first sends HTTP request asking for an upgrade. Server then responds with 101 Switching Protocol.

  • After the handshake succeeds, bi-directional communication can follow from either client to server or server to client.

Please check the diagram below:

WebSocket

WebSocket termination can be initiated by both the server and the client, and it's an essential aspect of managing WebSocket connections.

Server-Initiated Termination:
The WebSocket server can initiate the closure of a WebSocket connection. This can happen for various reasons, such as the server needing to perform maintenance or the application logic requiring the connection to be closed. The server sends a WebSocket Close frame to the client, indicating the intention to terminate the connection.

Client-Initiated Termination:
The WebSocket client can also initiate the termination of the connection. This might occur if the user decides to close a chat window or navigate away from a web page using WebSocket. In this case, the client sends a Close frame to the server to request the termination of the connection.

WebSocket termination is essential for releasing resources and maintaining the cleanliness of the connection pool, especially in scenarios where numerous clients and connections are involved. Termination ensures that both the server and the client can gracefully close the connection, allowing resources to be freed and preventing potential memory leaks.

Conclusion

In conclusion, this article has explored four distinct methods of achieving real-time communication between clients and servers, each with its own characteristics and use cases. Let's summarize each of them:

Short Polling: In short polling, the client repeatedly sends requests to the server at regular intervals to check for updates or new data. The server responds immediately, even if there is no new information available. While simple to implement, short polling can result in unnecessary network traffic.

Long Polling: Long polling involves the client sending a request to the server, and the server holds onto the request, waiting for updates. It responds as soon as new data is available or after a timeout. Long polling reduces unnecessary requests but still introduces latency.

Server-Sent Events (SSE): SSE establishes a persistent unidirectional connection from the client to the server using standard HTTP. The server sends events or updates to the client as they occur. SSE is efficient for server-to-client communication without the need for frequent polling.

Web Sockets: Web Socket is a full-duplex, bidirectional communication protocol that allows real-time, low-latency data exchange between clients and servers. It starts with a Web Socket handshake, and once established, it facilitates efficient and persistent bidirectional data flow.

The choice of which method to use depends on the specific requirements of the application. Short polling and long polling are suitable for scenarios with less stringent real-time demands, while SSE and Web Sockets provide more efficient solutions for applications that require immediate data updates and low latency.

Ultimately, the selection of a real-time communication method should be based on the specific needs of your application, balancing factors such as data freshness, network efficiency, and the complexity of implementation.

Follow me for more such content:
LinkedIn: https://www.linkedin.com/in/shameeluddin/
Github: https://github.com/Shameel123

Top comments (2)

Collapse
 
miketalbot profile image
Mike Talbot ⭐

The primary reason to use polling is that you are operating in network environments that break WebSockets and SSE. Fallbacks to these techniques are important strategies.

Collapse
 
shameel profile image
Shameel Uddin

Indeed.