Server-Sent Events Pattern
ELI5 — The Vibe Check
Server-Sent Events (SSE) is a one-way stream from server to browser over plain HTTP. The server pushes updates, the client just listens. No WebSocket complexity, automatic reconnection built in, works through proxies. Perfect for notifications, live feeds, and dashboards where the client just needs to receive.
Real Talk
Server-Sent Events is an HTTP-based protocol for server-to-client streaming using the EventSource API. It uses text/event-stream content type, supports automatic reconnection, event IDs for resumption, and named event types. Simpler than WebSockets for unidirectional use cases.
Show Me The Code
// Client
const source = new EventSource('/api/events');
source.onmessage = (e) => console.log(e.data);
source.addEventListener('notification', (e) => {
showNotification(JSON.parse(e.data));
});
When You'll Hear This
"SSE powers our real-time dashboard — the server pushes metric updates every second." / "SSE auto-reconnects when the connection drops — one less thing to implement."
Related Terms
Long Polling Pattern
Long Polling is polling's smarter cousin. Instead of asking 'any updates?
Streaming Protocol
Streaming Protocols deliver data continuously instead of in one big chunk.
WebSocket
WebSocket is like upgrading a walkie-talkie from push-to-talk to a full phone call.