Skip to content
Velloreth
Web Development 3 min read

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

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

TechniqueDirectionConnectionComplexity
Pollingpulla new requestlow
Long pollingpulla held requestlow
Server-sent eventspushone, one-waymedium
WebSocketbothone, both wayshigh

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.

For the related topics, see server-sent-events and websockets-explained. For the broader context, rest-api-rate-limiting and caching-strategies-guide.

Frequently asked questions

What is long polling?
A technique where the client sends a request and the server holds it open until there is data to send or a timeout is reached. The client then immediately sends another request. It simulates a push over plain HTTP, and it needs no special protocol.
What is the difference between long polling and a WebSocket?
A WebSocket is a persistent, bidirectional connection, so the server can push at any time and the client can send at any time. Long polling is a series of HTTP requests, so it is one-directional per request and it has the overhead of a new request each time. A WebSocket is more efficient and more complex.
Why does long polling need a timeout?
Because a proxy or a load balancer may close an idle connection, and 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. A common value is 30 seconds.
What is the reconnect backoff?
A delay before the client retries after a failure, which grows on each consecutive failure. It prevents a thundering herd when the server is down, because every client does not retry at once. A jitter is added so the retries are spread out.
When should I use long polling?
When a WebSocket is not available, such as behind a restrictive proxy, or when the update rate is low and the simplicity matters. For a high-frequency update or a bidirectional flow, a WebSocket or server-sent events is better.
What is the difference between long polling and server-sent events?
Server-sent events is a one-way stream over a single HTTP connection, so the server pushes and the client does not send. It reconnects automatically and it is simpler than a WebSocket. Long polling is a fallback for a client or a proxy that does not support it.