⭐ What is SSE?
SSE (Server-Sent Events) is a way for the server to send real-time updates to the client over a single HTTP connection.
✔ One-way communication
- Server → Client (Not client → server)
✔ Uses normal HTTP
- No special protocol like WebSockets
- Works everywhere
✔ Auto-reconnect
- If connection drops, browser reconnects automatically
✔ Text/event-stream format
- Very lightweight
⭐ How SSE works (super simple explanation)
- Client connects to server using a URL:
const eventSource = new EventSource("/events");
-
Server keeps the connection open.
-
Whenever there is new data, the server pushes it to the client:
data: New message!
- Browser receives it instantly:
eventSource.onmessage = (e) => {
console.log("Received:", e.data);
};
Easy explanation:
🟦 “Client opens a pipe. Server keeps sending updates through it.”
⭐ SSE vs WebSockets (super easy)
| Feature | SSE | WebSockets |
|---|---|---|
| Direction | One-way (server → client) | Two-way (both ways) |
| Protocol | HTTP | WebSocket protocol |
| Complexity | Very easy | Medium |
| Real-time | Yes | Yes (even faster) |
| Auto-reconnect | Yes | No (must handle manually) |
| Best for | Notifications, live scores | Chat, games, collaboration apps |
⭐ When to use SSE?
✔ Good for:
- Notifications
- Live score updates
- Stock price updates
- Real-time dashboard
- Streaming logs
- One-way events
✔ Not good for:
- Chat systems
- Multiplayer games *
Anything needing two-way communication
⭐ Why big companies use SSE?
- Lightweight compared to WebSockets
- One connection can push thousands of updates
- Superb for one-directional real-time feeds
- Ideal alternative if WebSockets are blocked (corporate networks)
⭐ Simple Server Example (Node.js)
app.get("/events", (req, res) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
setInterval(() => {
res.write(`data: ${new Date().toISOString()}\n\n`);
}, 1000);
});
⭐ Simple Client Example
const es = new EventSource("/events");
es.onmessage = (e) => {
console.log("Update:", e.data);
};
🎯 Final easy definition
SSE is a simple way for the server to push real-time updates to the client using a single long-lived HTTP connection, but only in one direction.