Three approaches
| Approach | Mechanism | Cost |
|---|---|---|
| Short polling | Poll on a timer for new data | Wasted requests, high latency |
| Long polling | Hold the request until data arrives | Rebuilds each time, server holds connections |
| WebSocket | Full-duplex long connection after one handshake | Connection needs upkeep, stateful |
How to choose
- Low frequency, tolerant delay: short polling is simplest;
- Near-real-time, one-way push: long polling suffices and is proxy-friendly;
- High-frequency two-way, chat or quotes: WebSocket fits best.
WebSocket notes
- Heartbeat: send pings or a proxy may drop the idle connection;
- Reconnect: on flaky networks, reconnect with exponential backoff to avoid hammering the server;
- Scale out: with multiple instances, a message bus broadcasts to the node holding that connection.