Pushing to the browser
Plain HTTP is request response, so getting live server updates to a client needs a technique. Three dominate.
- Long polling has the client send a request that the server holds open until it has data or a timeout, then the client immediately reconnects. It works everywhere but reopens a connection per message, adding overhead.
- Server sent events, or SSE, opens one long lived HTTP connection over which the server streams events. It is one directional server to client, auto reconnects, and is simple, but it carries only text and is limited by the browser per host connection cap on older HTTP.
- WebSockets upgrade the connection to a full duplex channel where both sides send messages anytime. It suits chat, games, and collaborative editing but adds protocol complexity and needs its own scaling and load balancing.
Choosing
- Need only server to client updates like a notification feed or live scores: prefer SSE for its simplicity.
- Need two way low latency messaging: pick WebSockets.
- Stuck with old proxies or want the simplest fallback: long polling still works.
Key idea
Use SSE for one way streaming WebSockets for full duplex messaging and long polling as the universal fallback when neither is available.