link: HTTP Polling

Long Polling

Overview

Long polling is a technique used in web applications to maintain a persistent connection between the client and server. Unlike Short Polling, which closes the connection after each request, long polling keeps the connection open until new data is available. This reduces the need for frequent requests and provides a near real-time update mechanism.

How Long Polling Works

  1. Client Request: The client sends a request to the server and keeps the connection open.
  2. Server Waits: The server holds the request open until new data is available or a timeout occurs.
  3. Server Response: When new data is available, the server responds with the updated information.
  4. Repeat: The client immediately sends a new request after receiving the response, maintaining the connection.

Pros/Cons

Pros

  • Efficiency: Reduces the number of requests compared to short polling, lowering server load and network traffic.
  • Reduced Latency: Provides near real-time updates without the need for continuous polling.
  • Simplicity: Easier to implement than WebSockets and doesn’t require special server configurations.

Cons

  • Timeouts: Requires careful management of timeouts to avoid connection drops.
  • Server Resources: Keeping connections open can consume server resources, especially with many concurrent clients.
  • Latency: While reduced compared to short polling, there is still some latency compared to more advanced real-time technologies.

Use Cases

Long polling is suitable for applications that require near real-time updates but cannot use WebSockets or Server-Sent Events. Examples include:

  • Chat Applications: Providing near real-time messaging without using WebSockets.
  • Notifications: Delivering updates or alerts as soon as they become available.
  • Live Feeds: Keeping data streams updated with minimal delay.

Example Implementation

Here’s a basic example of long polling using JavaScript and a RESTful API:

function longPoll() {
    fetch('https://api.example.com/data')
        .then(response => response.json())
        .then(data => {
            console.log('Data:', data);
            // Process the data
            longPoll(); // Immediately start a new request
        })
        .catch(error => {
            console.error('Error:', error);
            // Retry after a delay in case of error
            setTimeout(longPoll, 5000);
        });
}
 
// Start long polling
longPoll();