diagram.mmd — sequence
Long Polling Request Flow sequence diagram

Long polling is a technique for near-real-time server push over standard HTTP, where the client sends a request that the server deliberately holds open until new data is available — or a timeout expires — before responding, at which point the client immediately re-connects.

What the diagram shows

This sequence diagram illustrates the long polling cycle between a Client and a Server, including how it differs from both short polling and WebSockets:

1. Client connects: the client sends a standard HTTP GET request with a request timeout header (e.g., 30 seconds) and optionally a Last-Event-ID to indicate what was last received. 2. Server holds connection: instead of responding immediately, the server suspends the response and waits — either by registering a callback on an event source or by polling an internal queue. 3. Event arrives: when a new event occurs (e.g., a new chat message, a job status change), the server writes the response and sends it to the waiting client. 4. Timeout path: if no event arrives before the timeout, the server sends an empty 200 OK or 204 No Content. The client re-connects immediately. 5. Client reconnects: after receiving any response (event or timeout), the client issues a new long-poll request. From the outside, this looks like a continuous connection.

Why this matters

Long polling predates WebSockets and Server-Sent Events but remains widely used because it works over standard HTTP/1.1 with no special infrastructure — it traverses proxies, firewalls, and load balancers that might block persistent TCP connections. The trade-off is higher per-message overhead (a full HTTP request/response cycle per event batch) and challenges with connection management under high concurrency.

For full-duplex real-time communication, compare with WebSocket Connection Flow. For server-initiated push without client reconnect overhead, see the networking SSE Event Stream diagram. The Serverless Request Flow illustrates why serverless functions are a poor fit for long polling — held connections consume the function's execution time budget.

Free online editor
Edit this diagram in Graphlet
Fork, modify, and export to SVG or PNG. No sign-up required.
Open in Graphlet →

Frequently asked questions

Long polling is a technique for near-real-time server push over standard HTTP, where the client sends a request that the server deliberately holds open until new data is available or a timeout expires. When the server responds (with data or a timeout signal), the client immediately issues a new request — creating the appearance of a continuous connection without requiring persistent TCP or WebSocket infrastructure.
The client sends an HTTP GET request with an optional timeout header and a `Last-Event-ID` to identify what was last received. The server suspends the response and waits for a new event from its internal event source or queue. When an event arrives, the server writes and sends the response. If the timeout expires without an event, the server sends an empty 200 or 204 response. Either way, the client re-connects immediately with a new request.
Use long polling when you need near-real-time updates but cannot rely on WebSocket or Server-Sent Events — for example, in environments with strict firewall or proxy rules that block persistent connections, or when you need to support HTTP/1.1 clients without upgrade capability. It is a pragmatic fallback when full-duplex connections are unavailable.
Long polling simulates push over standard HTTP request/response cycles with a reconnect loop — high per-event overhead but maximum infrastructure compatibility. WebSockets establish a persistent full-duplex TCP connection after an HTTP upgrade, enabling low-overhead bidirectional messaging. Server-Sent Events use a persistent HTTP connection for unidirectional server-to-client streaming without the reconnect overhead of long polling. Choose WebSockets for interactive two-way communication, SSE for server-to-client streams, and long polling only when the other two are not viable.
mermaid
sequenceDiagram participant C as Client participant S as Server C->>S: GET /events?since=last-event-id (timeout: 30s) S->>S: Register client in waiting connections map S->>S: Check event queue for pending events alt Events already queued S-->>C: 200 OK with event payload C->>C: Process received events C->>S: GET /events?since=new-last-event-id (immediate reconnect) else No events - hold connection open S->>S: Wait for event or timeout alt New event arrives before timeout S->>S: Retrieve event data S-->>C: 200 OK with event payload C->>C: Process received events C->>S: GET /events?since=new-last-event-id (immediate reconnect) else Timeout elapsed - no events S-->>C: 204 No Content C->>S: GET /events?since=last-event-id (reconnect) end end
Copied to clipboard