Real-Time Updates: Polling, SSE, WebSockets
How the server pushes fresh data (chat, live comments, notifications, dashboards, collaborative editing, driver locations).
| Transport | How | Pros | Cons / use when |
|---|---|---|---|
| Short polling | client asks every N s | trivial, works everywhere, stateless | wasted requests, latency ≤ N s; fine for slow-changing data |
| Long polling | server holds the request until data arrives (or ~30 s), client re-requests | near real time over plain HTTP | a request per message; reconnect storms |
| Server-Sent Events | one long-lived HTTP response streaming text/event-stream | simple, server → client, auto-reconnect with Last-Event-ID, works over HTTP/2 | one direction only; good for feeds, live comments, LLM token streaming |
| WebSockets | upgraded TCP connection, full duplex | low latency both ways | stateful connections: sticky routing, LB/proxy support, reconnection logic; chat, games, collaboration |
| WebRTC | peer-to-peer (STUN/TURN for NAT traversal) | lowest latency, offloads servers | complex; audio/video calls |
The server side is the real design problem:
publishers -> pub/sub (Redis pub/sub, Kafka, NATS) -> gateway servers holding connections -> clients
^
connection registry: user_id -> gateway (Redis, with TTL heartbeats)
- Connections are stateful: track which gateway holds each user; gateways subscribe only to the topics their users need.
- Plan for reconnects: clients resume from the last event id / sequence number and fetch what they missed from durable storage.
- One gateway holds tens to hundreds of thousands of mostly idle connections (memory and file descriptors per connection); scale horizontally and drain connections gracefully on deploys.