Real-Time Backend
Server-Sent Events
Цели урока
- Understand how SSE works over HTTP (chunked encoding, text/event-stream)
- Use the four format fields: data, event, id, retry
- Build an SSE server in Node.js with auto-reconnect and Last-Event-ID
- Choose between SSE, WebSocket, and polling based on requirements
Предварительные знания
2013. Twitter moved its live feed from polling to SSE - server load dropped 10x. One HTTP request replaced thousands of repeats, and new tweets surfaced instantly. 2022: ChatGPT shipped its typing effect on SSE, streaming each token as its own event. SSE is the first real push technology baked straight into the browser.
- Facebook/Twitter - live feeds and notifications via SSE
- GitHub - real-time CI/CD log streaming
- Financial platforms - quote and price updates
- ChatGPT - streaming model responses (the typing effect via SSE)
Ian Hickson and the HTML5 EventSource
Ian Hickson, lead editor of the HTML5 specification at WHATWG, added Server-Sent Events to the HTML5 draft in 2006. Before that, the only way to receive server events was polling or long polling - workarounds bolted on top of HTTP. Hickson wanted browsers to ship a native push mechanism without leaning on WebSocket, which lived in a separate spec. In 2014 SSE was published as a standalone W3C standard.
SSE: Push over ordinary HTTP
2013. Twitter moves its live feed from polling to SSE - server load drops 10x. One HTTP request replaces thousands of repeats. ChatGPT in 2022 uses SSE for the typing effect, streaming each token as its own event. That is SSE: the server pushes events to the client, not the other way around.