Long Polling
The ways a client learns that something changed on a server, from asking repeatedly, to holding a request open until there's news, to the push channels that replaced both.
Polling is the simplest way for a client to learn about changes on a server: ask repeatedly. Long polling is the trick that made it feel instant. Both exist because HTTP was designed so that only the client can start a conversation, while many applications need the server to speak first, to say a message arrived, a build finished, or a price changed.
There are several approaches, in roughly historical order:
- Short polling. The client asks "anything new?" on a fixed interval. It's easy and works everywhere, but it trades freshness against waste: poll every 30 seconds and updates arrive up to 30 seconds late, poll every second and most requests return nothing while costing a full round trip, headers included.
- Long polling. The client sends a request, and the server doesn't answer until it has something to say or a timeout passes, often around 30 seconds. On a response, the client immediately sends the next request. Updates arrive almost as soon as they happen, and empty responses become rare. Gmail's chat and early Facebook chat relied on it in the late 2000s, when the family of server-push techniques was nicknamed Comet.
- Server-sent events. One long HTTP response stays open and the server writes events down it as they happen, as described in Streaming Response. It's one-way, server to client, and reconnects automatically.
- WebSocket. A full two-way connection upgraded from HTTP, for chat, games, and collaborative editing.
Long polling has costs that pushed the web toward the later options. Each waiting client holds a connection and, on older thread-per-request servers, a whole thread, so a server with thousands of idle listeners exhausted its resources. That pressure drove the move to servers built on an Event Loop, which can hold many quiet connections cheaply. Messages can also be lost or duplicated in the gap between one response and the next request, so clients usually send the ID of the last event they saw, and the system needs the Delivery Guarantees thinking that any messaging does.
Polling hasn't gone away. It survives proxies and Firewall rules that break long-lived connections, it needs no special server support, and it's stateless, so any server behind a Load Balancing layer can answer. Many systems combine the two: a push channel for speed and an occasional poll to catch anything missed. Between servers, a Webhook reverses the direction entirely, so the provider calls the consumer instead.
Polling well means being polite. Clients should back off when nothing changes or the server struggles, using Exponential Backoff with random jitter, since thousands of clients polling in lockstep produce a thundering herd. Browsers throttle timers in hidden tabs, which slows polling too, a deliberate saving described in Background Tab Throttling.
See also3
File Watching
How programs learn that files changed, by polling or by kernel notification interfaces such as inotify and FSEvents, and the many ways those notifications get lost, delayed, or exhausted.
Systems & Tooling8 connections
MQTT
A lightweight publish-subscribe protocol in which devices send small messages to topics on a central broker, the common language of sensors, smart homes, and industrial telemetry.
Networks & Distribution9 connections
Optimistic UI
Updating the UI as if a server request has already succeeded, before the server has actually responded.
Web Platform21 connections
Linked from8
- Background Tab ThrottlingWeb Platform
Browsers deliberately slow, freeze, or discard pages that are not visible, so code that assumes it keeps running in the background will be wrong.
- Exponential BackoffNetworks & Distribution
Waiting longer after each failed attempt, with a dose of randomness, so retries give a struggling system room to recover instead of hammering it.
- File WatchingSystems & Tooling
How programs learn that files changed, by polling or by kernel notification interfaces such as inotify and FSEvents, and the many ways those notifications get lost, delayed, or exhausted.
- MQTTNetworks & Distribution
A lightweight publish-subscribe protocol in which devices send small messages to topics on a central broker, the common language of sensors, smart homes, and industrial telemetry.
- Optimistic UIWeb Platform
Updating the UI as if a server request has already succeeded, before the server has actually responded.
- Streaming ResponseNetworks & Distribution
Delivering a response incrementally as it is produced rather than all at once when complete.
- WebhookNetworks & Distribution
A webhook is an HTTP callback a service pushes to you when an event happens, inverting who initiates the request.
- WebSocketWeb Platform
A persistent, full-duplex connection between browser and server, replacing repeated request-response with an open channel.