Long Polling is a client-server interaction technique where the server holds an HTTP request open until new data becomes available or a timeout expires. Unlike periodic polling, the server does not return an empty response immediately but waits for an event to occur before sending data to the client. According to MDN Web Docs, 2024, Long Polling remains a popular solution for real-time applications where WebSocket is unavailable or excessive.
Key Takeaways
Long Polling is a communication pattern in a client-server architecture where a client initiates an HTTP request, and the server delays sending a response until new data becomes available or a specified timeout expires. After receiving the response, the client immediately sends the next request, creating the effect of a continuous connection.
The Long Polling technique emerged as an evolutionary development of Short Polling to reduce the number of empty HTTP requests. In traditional polling, the client sends requests every N seconds, and the server responds even when there is no new data. In Long Polling, the server uses a connection holding mechanism, which dramatically reduces useless traffic.
Before the advent of WebSocket in 2011, Long Polling was the primary method for real-time communication on the web. Companies such as Facebook and Gmail used this technique for their chats and notifications in the early 2010s. According to High Performance Browser Networking (Grigorik, 2013), Long Polling handled up to 95% of all real-time connections in major web applications of that period.
A client sends a standard HTTP request to the server. Upon receiving the request, the server does not return a response immediately — it places the request in a waiting queue. When an event occurs on the server (a new message, data change), the server forms a response and sends it to the client. Upon receiving the response, the client immediately creates a new Long Polling request, and the cycle repeats.
Long Polling works according to the following sequence of steps. The client sends an HTTP GET request to a server endpoint. Upon receiving the request, the server checks for new data in the event queue. If there is no data, the server holds the request in a waiting state, not sending a response immediately. The holding mechanism depends on the server implementation — most often asynchronous processing with callbacks or event-driven architecture is used.
When an event occurs on the server side (for example, a user sent a message in a chat), the server forms an HTTP response with a body containing this data and terminates the connection. The client receives the response, processes the data, and immediately initiates a new request. If no data appears during the waiting period, the server sends an empty response after the timeout expires, and the client also re-establishes the connection. The timeout is usually 30–60 seconds for a balance between load and latency.
A key configuration parameter for Long Polling is the waiting timeout. A timeout that is too short (less than 10 seconds) leads to an increase in the number of requests, bringing the technique closer to Short Polling. A timeout that is too long (more than 120 seconds) may cause connection breaks by intermediate proxies and load balancers. The recommended value for most scenarios is 30–45 seconds.
If multiple events occur on the server during a single Long Polling request, the server must transmit them all in one response or organize an event queue on the client side. For this purpose, event buffering is used: the server accumulates events that occurred during the holding time of the request and transmits them as an array of data in the response body.
Let’s look at a simple Long Polling implementation on the client side using the modern Fetch API. The client function sends a request and recursively calls itself after receiving a response.
async function longPoll(url) {
try {
const response = await fetch(url);
const data = await response.json();
handleData(data);
longPoll(url);
} catch (error) {
console.error("Long Polling error", error);
setTimeout(() => longPoll(url), 3000);
}
}
function handleData(data) {
if (data.events && data.events.length > 0) {
data.events.forEach(event => {
console.log("New event:", event);
});
}
}
longPoll("/api/events");
This code creates an infinite Long Polling loop: after receiving a response, the function immediately sends a new request. In case of a connection error, a three-second delay is set before retrying to avoid avalanche load on the server.
On the server side, it is necessary to hold the request until an event occurs or a timeout expires. An implementation example using EventEmitter in Node.js demonstrates this mechanism.
const express = require("express");
const EventEmitter = require("events");
const app = express();
const eventBus = new EventEmitter();
app.get("/api/events", (req, res) => {
const timeout = setTimeout(() => {
res.json({ events: [] });
}, 30000);
eventBus.once("new-event", (data) => {
clearTimeout(timeout);
res.json({ events: [data] });
});
});
app.post("/api/events", (req, res) => {
eventBus.emit("new-event", req.body);
res.send({ status: "ok" });
});
app.listen(3000);
The server part uses EventEmitter to notify waiting Long Polling connections when new data appears. Upon reaching the 30-second timeout, the server returns an empty array of events, and the client creates a new request.
Long Polling is used in scenarios where real-time data delivery is required but using WebSocket is impossible due to technical or infrastructural reasons. The most common cases are corporate proxies and firewalls that block WebSocket connections, as well as environments with limited protocol support on the server side.
The key factor in choosing Long Polling is backward compatibility. All HTTP clients and servers support this method, making it a universal solution for real-time functionality without additional dependencies. According to HTTP Archive (2024), about 8% of all websites continue to use Long Polling for basic real-time functionality.
Long Polling and Short Polling solve the same problem — data delivery from server to client — but fundamentally differ in mechanism and efficiency. Short Polling uses a fixed polling interval where the client sends HTTP requests at equal time intervals regardless of whether new data has appeared on the server.
| Characteristic | Long Polling | Short Polling |
|---|---|---|
| Response initiation | Server sends data on event | Server responds to each client request |
| Delivery latency | Minimal, up to 1 second | Depends on polling interval, 3–60 seconds |
| Number of requests | 1 request per event or timeout | N requests per unit of time (fixed) |
| Idle traffic | Low (one open request) | High (requests every N seconds) |
| Server load | Holding connections | Processing frequent requests |
| Implementation complexity | Medium (asynchronous processing) | Low (regular HTTP requests) |
Short Polling is simpler to implement but creates significantly more load on the server and network with the same data update frequency. If a latency of less than 5 seconds is required, Short Polling generates dozens of requests per minute, while Long Polling uses one request per event or timeout. For applications with infrequent events, Long Polling is orders of magnitude more traffic-efficient.
WebSocket is a full-fledged bidirectional real-time protocol operating over TCP after an initial HTTP handshake. Unlike Long Polling, WebSocket establishes a single persistent connection and allows the server to send data to the client at any time without creating a new HTTP request.
The choice between Long Polling and WebSocket depends on several factors. Compatibility: Long Polling works through any proxies and firewalls, while WebSocket may be blocked by corporate networks. Performance: WebSocket has lower overhead (2 bytes per frame versus full HTTP headers), which is critical at high message frequency. Scalability: Long Polling requires more server-side resources due to holding many connections, while WebSocket uses a fixed connection per session.
According to Mozilla Developer Network (2024), WebSocket is supported by all modern browsers since versions 2011–2015, but corporate proxies (e.g., Symantec Blue Coat) continue to block it in 15–20% of corporate networks, which keeps Long Polling relevant as a fallback solution.
Frequently Asked Questions
Long Polling is when a client asks the server: “respond when new data becomes available,” and the server keeps the connection open, waiting for an event. As soon as data appears, the server responds, and the client immediately asks the same question again.
With Short Polling, the client asks the server every N seconds whether there is data, even if there isn’t. With Long Polling, the client asks once, and the server only responds when data actually becomes available. Long Polling creates fewer empty requests and reduces network load.
Long Polling should be used when WebSocket is unavailable: in corporate networks that block non-HTTP protocols, when backward compatibility with older browsers is needed, or when there are hosting-side limitations. WebSocket is more efficient for high-frequency data exchange.
The recommended Long Polling timeout is 30–45 seconds. A lower value (10–15 seconds) increases the number of requests, while a higher value (60+ seconds) risks connection breaks by intermediate load balancers. The timeout value depends on the network architecture and latency requirements.
The main disadvantages of Long Polling are high memory consumption on the server when holding thousands of connections, difficulty in horizontal scaling (requires a centralized event queue), and the lack of true bidirectional communication — separate POST requests are needed to send data to the server.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also