How it works
Ordinary HTTP works well for login and profile changes and can also send a message. For live delivery, short polling repeatedly asks for updates, creating empty requests when traffic is quiet: one million open apps polling every 5 seconds make 200,000 requests per second. Long polling waits for an event or timeout before the client opens another request, as Telegram’s Bot API does with getUpdates. It reduces empty replies but still needs reconnect and timeout handling.
Server-sent events (SSE) keep one HTTP response open and stream events from server to client. Browsers support them through EventSource, which reconnects automatically and reports the last event ID it saw. They suit one-way feeds, such as the token streams from LLM APIs. Chat can use them too, with sends going over plain HTTP requests.
A WebSocket starts with a client handshake, an HTTP request asking to upgrade, and then holds one bidirectional channel carrying messages both ways. It suits frequent asynchronous chat updates, but the server must manage many live connections, heartbeats (small periodic messages proving the peer is still there), reconnects, backpressure (slowing output a client cannot absorb), and refreshing credentials that expire while the socket stays open. A persistent channel is a delivery path, not durable storage.
Use the simplest transport that meets the product’s latency and scale goals. This chapter uses WebSocket for live send and receive while keeping routine account APIs on HTTP. Mobile push wakes an inactive device; it is a notification path and may be delayed or disabled.
A support inbox gets one update every few hours, while a game lobby exchanges updates each second. Would both need WebSocket?
Probably not. A quiet inbox can begin with polling, long polling, or server-sent events if latency targets permit. Frequent lobby updates favor a persistent bidirectional connection. Measure connection and request cost for the actual workload.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Polling repeats requests, long polling holds them, SSE streams one way, and WebSocket keeps a two-way channel.
- Choose transport separately from persistence and push notification.