Choose from direction, urgency, and connection lifetime#
“Real time” is not a protocol requirement. Ask who initiates updates, whether the client sends independently, how much delay is acceptable, how many connections must remain open, and what happens after a disconnect.
Four useful mechanisms#
Polling
The client asks periodically. It works with ordinary HTTP infrastructure and is easy to operate, but empty checks waste work and update latency follows the interval.
Client → Server: Any updates?
Client → Server: Any updates?
Client → Server: Any updates?Long polling
The server holds a request until data is available or a timeout occurs, then the client immediately reconnects. It approximates server push while retaining repeated HTTP requests.
Server-Sent Events
SSE maintains an HTTP response stream from server to browser. It is one-directional and browser EventSource handles reconnection behavior. It fits notifications, dashboards, and streamed server updates when client messages can use normal requests.
WebSockets
A WebSocket is a long-lived bidirectional channel. Client and server can send independently, so chat, presence, multiplayer, and live collaboration may fit. The application still owns authentication refresh, heartbeats, idle policy, backpressure, reconnect, and missed-event recovery.
| Mechanism | Direction | Connection | Best fit |
|---|---|---|---|
| Polling | Client requests | Repeated | Simple or infrequent updates |
| Long polling | Mostly server response | Repeated long requests | Moderate real-time needs |
| SSE | Server → client | Persistent | Server-pushed updates |
| WebSocket | Bidirectional | Persistent | Interactive real-time systems |
The hard part starts after the connection opens#
User A may be connected to Gateway A while User B is connected to Gateway B. The system needs a presence or connection registry to locate B and a routing path, often Pub/Sub or a broker, between gateways. Sticky routing can help reconnect a client to a gateway but does not route messages between different users.
- Choosing WebSockets only because a requirement says “real time.”
- Assuming the connection itself routes across gateway servers.
- Ignoring reconnect, presence expiration, connection limits, and rolling deployments.
- Introducing a streaming platform before a routing requirement justifies it.
Use these prompts to extend the design. The useful answer is a reasoned change, not a memorized sentence.
- How many persistent connections can one gateway support?
- How does a message reach a user connected elsewhere?
- What events are replayed after reconnect?
- How is presence expired after a silent disconnect?
- What happens during a rolling deployment?
Related practice problems
Use the manifest-backed prompts below to apply this lesson in a complete design.
- RFC 6455: The WebSocket ProtocolIETF / RFC Editor
- Using server-sent eventsMDN Web Docs