Server-Sent Events (SSE)
Updated June 6, 2026HTTP follows a strict request-response cycle. The client asks a question, the server answers. If the server gets new information, it has no way to notify the client without being asked first.
If you're building a live stock ticker, the naive approach is polling: the frontend sends a GET request every 2 seconds asking "any new prices?" This is inefficient. 90% of the time the answer is "No," so you're wasting battery, bandwidth, and server CPU on empty conversations.
Server-Sent Events (SSE) solve this with a simpler, lighter mechanism than WebSockets: a one-way persistent stream from server to client.
How SSE Works
SSE allows a client to open a single HTTP connection to a server. Instead of the server responding immediately and closing the connection (like a normal API call), the server keeps the connection open. Whenever an event happens on the backend, the server pushes a text message down that open pipe.
The client opens the connection using the browser's built-in EventSource API:
const eventSource = new EventSource('/api/stocks/live');
eventSource.onmessage = (event) => {
console.log("New stock price:", event.data);
};On the backend, the server responds with Content-Type: text/event-stream. It then streams data chunks separated by double newlines:
data: {"symbol": "AAPL", "price": 150.00}
data: {"symbol": "GOOG", "price": 2800.00}SSE flow — client subscribes once, server pushes events over a single HTTP connection
SSE vs WebSockets
If you want real-time features, you usually choose between SSE and WebSockets.
SSE is the right choice when:
The data flows only one way — server to client. Live news feeds, sports score tickers, and AI text generation are all server-push scenarios. The client only needs to listen.
SSE uses standard HTTP/1.1 or HTTP/2. WebSockets require a protocol upgrade to a custom TCP connection. This means SSE works through corporate firewalls, load balancers, and proxy servers without special configuration.
WebSockets are the right choice when:
The client also needs to send data back to the server frequently. A multiplayer game or a chat application needs bidirectional communication — SSE doesn't support that.
What is the key difference between SSE and WebSockets?
Built-in Reconnection
One of SSE's most useful properties is automatic reconnection. If a WebSocket drops because a user goes through a tunnel, the developer has to write custom logic to reconnect.
With SSE, the browser's EventSource API reconnects automatically. More importantly, each event can include an id field:
id: 42
data: {"symbol": "AAPL", "price": 150.00}The browser tracks the last received ID. On reconnect, it sends a Last-Event-ID header so the server knows exactly where the client left off and can resume the stream without gaps.
Auto-reconnect with Last-Event-ID — no missed events after a network blip
How does SSE handle reconnection when a network connection drops?
The AI Streaming Use Case
For years, SSE was underappreciated. Then came large language models.
When you ask ChatGPT a question, it doesn't generate the full 500-word answer at once. It generates one token at a time. If the UI waited for the complete response, you'd stare at a loading spinner for 10 seconds.
OpenAI and nearly every AI tool use Server-Sent Events to stream tokens to the frontend the moment they're generated, creating the familiar "typing" effect. Since the user is just reading (not sending data back during generation), SSE is the right fit here: simpler than WebSockets, works over plain HTTP, and handles reconnection automatically.
When to Use SSE
Use SSE when:
- You need server-to-client push and the client doesn't send data back
- You want simple infrastructure with no protocol upgrade
- You need built-in reconnection and event replay (via
Last-Event-ID) - You're streaming AI responses or live notifications
Use WebSockets when:
- The client sends data to the server frequently (chat, gaming, collaborative editing)
- You need very high message frequency in both directions
Why do AI tools like ChatGPT use SSE to stream responses instead of returning a single HTTP response?
Summary
Server-Sent Events provide a unidirectional, real-time data stream from server to client over a standard HTTP connection. They solve polling by eliminating empty requests. Compared to WebSockets, SSE is simpler, works over plain HTTP, and features automatic reconnection with event replay. The modern use case that made SSE widely known is streaming AI token generation. For any one-way server-push scenario where the client only listens, SSE is often the better choice than WebSockets.
Saved on this device only
Sign in to sync progress across devices