Understanding Short Polling, Long Polling, Server Sent Events and Web Sockets
Tags: javascript, node, tutorial, beginners
Published: 2023-12-14 06:49:54


## Table of Contents

1. [Introduction](#introduction)
2. [Short Polling](#short-polling)
3. [Long Polling](#long-polling)
4. [SSE](#sse)
5. [Web Sockets](#web-sockets)
6. [Conclusion](#conclusion)


## Introduction <a name="#introduction"></a>

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  <a name="#short-polling"></a>

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ji9at0eirn4nyvk6mv96.png)




## Long Polling <a name="#long-polling"></a>

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3rhf5geenbpqph71eum.png)



## SSE <a name="#sse"></a>

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzzpe55gkazx6wggunsq.png)





## Web Sockets <a name="#web-sockets"></a>

- 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 ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyqzz7mczty9985t72al.png)



**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 <a name="#conclusion"></a>

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