How do polling, long polling, SSE and WebSockets differ for real-time updates?
Polling asks repeatedly; long polling holds each request open until there is news; SSE streams server-to-client over one HTTP connection; WebSockets give a two-way channel. Choose by direction of data and scale.
Explain it simply
Imagine waiting for a train status.
- Polling: you keep asking "has it arrived?" every 5 seconds. Simple, but wasteful and delayed.
- Long polling: you ask, and the clerk does not answer until there is news (or a timeout), then you ask again. Fewer empty replies, still one request per update.
- Server-Sent Events (SSE): the server keeps one HTTP connection open and streams updates to you. One-way (server to client), auto-reconnect built in, works with normal HTTP infrastructure. Ideal for live scores or notifications.
- WebSockets: a persistent two-way channel after an HTTP upgrade. Both sides send any time. Best for chat, collaborative editing, multiplayer games.
Trade-offs: persistent connections consume server memory and need load balancers that support them, and clients must handle reconnects. Polling is stateless and easiest to scale, which is why it is still fine for slow-changing data.
Example
Choosing for three features:
Live cricket score (server -> client only) -> SSE
Chat between two users (both directions) -> WebSocket
Order status checked every few minutes -> plain pollingWhat to say in the interview
"Polling is simplest but wasteful, long polling reduces empty replies, SSE is a one-way stream over HTTP, and WebSockets are full duplex. I pick SSE for server push, WebSockets for two-way interaction, and polling when updates are rare. Persistent connections need extra capacity planning."