Long Polling Explained: Real-Time Without WebSockets
Long polling holds a request open until there is data. Learn the mechanism, the timeout, the reconnect, and the alternatives.
By Velloreth
The mechanism
1. The client sends a request.
2. The server holds it open until there is data or a timeout.
3. The server responds with the data or an empty response.
4. The client immediately sends another request.
Client: GET /events
Server: (holds for 30s)
Server: 200 { "event": "new-message" }
Client: GET /events
Server: (holds)
It simulates a push over plain HTTP.
The server
app.get('/events', async (req, res) => {
const since = Number(req.query.since) || 0;
const event = await waitForEvent(since, 30000);
if (event) {
res.json({ event });
} else {
res.status(204).end(); // no content, the client reconnects
}
});
The server holds the request until an event or a timeout.
A 204 tells the client there is nothing, and it reconnects.
The client
async function poll(since) {
try {
const res = await fetch(`/events?since=${since}`);
if (res.status === 200) {
const { event } = await res.json();
handle(event);
since = event.id;
}
} catch (e) {
await sleep(backoff());
}
poll(since); // immediately again
}
The client reconnects immediately after a normal response.
A backoff is applied after a failure.
The timeout
A proxy or a load balancer may close an idle connection.
A client that waits forever never learns that the connection is gone.
A timeout shorter than the proxy’s idle timeout lets the client reconnect cleanly.
30 seconds is a common value.
A heartbeat is an alternative, where the server sends an empty event to keep the connection alive.
The reconnect
A backoff that grows on each failure.
function backoff(attempt) {
const base = Math.min(1000 * 2 ** attempt, 30000);
return base + Math.random() * 1000; // jitter
}
The jitter spreads the retries, so every client does not retry at once.
A thundering herd is what happens without it.
The cost
A connection per client, held open.
A server with 10,000 clients holds 10,000 connections, which is a lot of memory and file descriptors.
A new request per event, with the full header overhead.
A WebSocket holds one connection and sends a frame, which is much cheaper.
The alternatives
| Technique | Direction | Connection | Complexity |
|---|---|---|---|
| Polling | pull | a new request | low |
| Long polling | pull | a held request | low |
| Server-sent events | push | one, one-way | medium |
| WebSocket | both | one, both ways | high |
Server-sent events is a one-way stream over one connection, and it reconnects automatically.
A WebSocket is bidirectional and the most efficient.
Long polling is the fallback when neither is available.
The choice
Use long polling when:
- A WebSocket is blocked by a proxy
- The update rate is low
- The simplicity matters
- The client is old
Use a WebSocket when:
- The flow is bidirectional
- The update rate is high
- The latency matters
Use server-sent events when:
- The flow is one-way
- A simple stream is enough
Common mistakes
No timeout. A stuck connection.
No backoff. A thundering herd.
No jitter. A synchronised retry.
A held connection per client at scale. A resource limit.
No since cursor. A missed or a duplicated event.
A long poll that returns immediately. A busy loop.
No error handling. A silent stop.
A proxy idle timeout shorter than the poll. A dropped connection.
No heartbeat. A silent disconnect.
A WebSocket used where a poll would do. Unnecessary complexity.
A quotable summary
Long polling holds an HTTP request open until there is data or a timeout, and the client immediately sends another, which simulates a push over plain HTTP. It needs a timeout shorter than the proxy’s idle timeout and a reconnect with a backoff and a jitter, and it costs a connection per client, which limits the scale. Server-sent events or a WebSocket is better when it is available, and long polling is the fallback.
Related reading
For the related topics, see server-sent-events and websockets-explained. For the broader context, rest-api-rate-limiting and caching-strategies-guide.