Server-Sent Events (SSE)
A lightweight standard protocol that streams real-time text events unidirectionally from server to client over a single persistent HTTP connection
Table of Contents
Structure Diagram
What is Server-Sent Events (SSE)?
A lightweight standard protocol that streams real-time text events unidirectionally from server to client over a single persistent HTTP connection. It is a core concept in APIs & Networking within Web Dev.
Core Principles
Server-Sent Events (SSE) streams real-time updates from server to browser over a plain HTTP response with Content-Type: text/event-stream. Unlike WebSockets, SSE is unidirectional (Server → Client) and works seamlessly through standard HTTP proxies and firewalls.
The browser's native EventSource API automatically reconnects on network drops and sends the Last-Event-ID header so the server can replay missed events.
Practical Takeaways
- Choose SSE over WebSockets for one-way streams: For AI token streaming, live notifications, progress bars, or stock tickers, SSE is far simpler to scale, authenticate, and debug than full-duplex WebSockets.
- Always serve over HTTP/2+: HTTP/1.1 limits browsers to ~6 concurrent connections per origin, which multiple open tabs can quickly exhaust. HTTP/2 multiplexes SSE streams over a single TCP connection.
- Prevent proxy buffering & idle timeouts: Send
X-Accel-Buffering: no(for Nginx) and emit periodic heartbeat comments (: keepalive\n\n) so load balancers don't drop idle streams.
Notable Examples
- LLM chat streaming (OpenAI, Anthropic, Gemini APIs): Streams generated tokens incrementally over SSE so users read responses as they are produced.
- Live CI/CD logs and activity feeds: Pushes real-time status updates without polling overhead.