We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
This idea carries a full system design question on its own. Each walks through the full answer.
System Design Masterclass · Messaging Event Systems
Free preview: the first 2 parts of this 7-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Messaging Event Systems
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
You are watching a live sports score page. Goals, fouls, substitutions, they appear on your screen the moment they happen. You did not click refresh. You did not press any button. The server just pushed the update to you.
Now here is the question: do you need to send anything back to the server while watching scores? No. You are just receiving. One-way traffic, server to client.
This is exactly the scenario Server-Sent Events were designed for. Unlike WebSockets, which establish a two-way channel, SSE opens a one-way pipe from the server to the browser. The server pushes events whenever it wants. The client listens.
The strength of SSE is that it is built directly on . No protocol upgrade. No special handshake. No binary framing. Just a long-lived HTTP response that the server keeps writing to. Your existing HTTP infrastructure (load balancers, CDNs, proxies, caching layers) works with SSE out of the box. Try saying that about WebSockets.
ChatGPT and Claude use SSE to stream tokens to your browser as the model generates them, word by word. That "typing" effect you see? That is SSE delivering one token at a time over a single HTTP connection.
The client opens a connection using the browser's built-in EventSource API:
const source = new EventSource('/api/events');
source.onmessage = (event) => {
console.log('New event:', event.data);
};
The server responds with Content-Type: text/event-stream and keeps the connection open:
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive
data: {"score": "1-0", "minute": 23}
data: {"score": "1-1", "minute": 45}
data: {"score": "2-1", "minute": 67}
Each event is a plain text block starting with data: and separated by blank lines. That is it. No binary frames, no opcodes, no masking. Just text over .
Built-in features that WebSockets lack:
Last-Event-ID header, and the server can replay missed events from that point.event: price-update, event: trade-executed), and the client can listen to specific types using addEventListener.| Feature | SSE | |
|---|---|---|
| Direction | Server to client only | Bidirectional |
| Protocol | HTTP | WebSocket (upgraded from HTTP) |
| Reconnection | Automatic (built-in) | Manual (you implement it) |
| Data format | Text only (UTF-8) | Text and binary |
| Through proxies | Almost always works | Sometimes blocked |
| Browser API | EventSource (simple) | WebSocket (low-level) |