Course map

Topic 040 / Core

Real-time communication: polling, long polling, WebSockets, SSE, webhooks

Chapter 3.7 · Real-Time Communication

What and why

Plain request/response (→ 28) only delivers data when the client asks; many features need the server to push new data as soon as it exists, without the client guessing when to ask again. This topic is a family of mechanisms for server-to-client (or server-to-server) push, trading simplicity against latency and connection cost: short polling (client re-asks on a timer), long polling (server holds the request open until data arrives or a timeout), SSE (one-way server-to-client stream with built-in auto-reconnect), WebSockets (full-duplex persistent connection), webhooks (server-to-server push to another server's endpoint, not a browser). Builds on: → 28.

Diagram

Mechanism Direction Connection cost Typical use
Short polling client pulls one request per interval infrequent updates
Long polling client pulls, server holds one held request per cycle moderate-latency updates
SSE server pushes one persistent connection, one-way feeds, notifications
WebSockets both push one persistent connection, full-duplex chat, games, collaboration
Webhooks server pushes to server none held; a POST per event server-to-server callbacks

How it works

  1. Short polling: the client sends a request every fixed interval regardless of whether anything changed — simplest, but wastes requests when nothing's new.
  2. Long polling: the client sends a request and the server holds it open, without responding, until new data exists or a timeout elapses; the client immediately reopens another request after each response.
  3. SSE: the client opens one HTTP connection and the server streams events down it as they occur; the browser reconnects automatically if the connection drops.
  4. WebSockets: client and server upgrade an HTTP connection into a persistent, full-duplex socket; either side can send at any time.
  5. Webhooks: instead of a client pulling, the receiving server registers a URL and the sending server POSTs to it when an event occurs; the receiver must verify the sender's signature and tolerate duplicate or out-of-order deliveries.

Practical example

(illustrative). A notification badge that can be a few seconds stale is fine with long polling or SSE. A collaborative document needing sub-second two-way updates needs WebSockets. A payment provider notifying your server that a charge completed uses a webhook, which your endpoint must verify and may receive more than once (Project 8).

When to use it

  • Use short polling when: updates are infrequent and a few seconds of staleness is fine — simplest to build and operate.
  • Use long polling or SSE when: one-way server push is needed without full duplex.
  • Use WebSockets when: the feature genuinely needs two-way, low-latency, high-frequency exchange (chat, games, live collaboration, Project 12) — and only then, because the persistent connection costs memory per connection, needs load-balancer affinity or a shared presence store, and risks reconnect storms when many clients disconnect and retry at once.
  • Use webhooks when: the receiver is another server, not a browser.
  • Avoid when: defaulting to WebSockets "because it's real-time" when polling at a sensible interval already meets the actual latency requirement.
  • Failure scenarios: a webhook receiver that doesn't verify the sender's signature can be forged; one that isn't idempotent (→ 38's sibling concern) double-processes a duplicate delivery; a WebSocket tier that scales memory linearly per connection, with no plan for a reconnect storm, falls over under one.
  • Simpler alternative: polling, at an interval matched to the actual latency requirement — correct more often than it is fashionable.

Practice

(decision kind — judge 4 scenarios; 20 min, no lab services). For each, choose a mechanism and defend it against the "polling is correct more often than it is fashionable" principle — justify any choice beyond polling with a stated latency requirement it meets: (1) a notification badge; (2) a live chat; (3) a dashboard refreshing once a minute; (4) a third-party payment-status callback. Done when every choice beyond polling cites the latency requirement that justifies it.

Key takeaway

Each mechanism trades latency and freshness against connection cost; default to the cheapest one that meets the actual requirement, and reserve WebSockets for genuine full-duplex, low-latency needs. Done when you can: pick a mechanism for a stated latency requirement and name the specific cost you're accepting for it. Used next in → 73, 114 (Project 12: message storage partitioning and ordering); → 116 (webhook idempotency connects to idempotent consumers).

References

None — this topic makes no external claims.

Source directory

Search the guide