Server-Sent Events (SSE) provide a lightweight, standardized way for a server to push one-way real-time updates to a client over a persistent HTTP connection. Unlike traditional HTTP requests which are short-lived, SSE maintains an open connection, allowing the server to continuously stream data without the client needing to repeatedly poll. While often discussed alongside WebSockets, SSE serves a distinct purpose: it's optimized for scenarios where only server-to-client communication is required, simplifying the implementation by building on standard HTTP rather than a full-duplex protocol.
Practically, SSE shines in applications needing live data feeds, such as stock tickers, news updates, progress bars, notifications, or activity streams, where the client primarily consumes information. Its simplicity is a major advantage: no complex protocol handshakes like WebSockets, and it benefits from browser-native EventSource API support, which includes automatic reconnection logic. This makes it an excellent choice when you need a simple, efficient mechanism for pushing data unidirectionally from your backend to frontend.
Under the hood, an SSE connection is initiated by the client making a regular HTTP request, and the server responds with a Content-Type: text/event-stream header, keeping the connection open. Data is then sent in a specific format (e.g., data: message) and parsed by the client's EventSource object. For an advanced developer, understanding when to choose SSE over WebSockets is key: if your feature strictly involves the server pushing updates to the client without requiring immediate client-to-server real-time feedback, SSE offers a less complex and often more performant solution than a full WebSocket implementation.
Key Takeaways
- Provides one-way, real-time data streaming from server to client.
- Built on standard HTTP, simpler than WebSockets for unidirectional communication.
- Browser's
EventSourceAPI offers native support, including automatic reconnection. - Ideal for live feeds, notifications, and progress updates where client-to-server real-time interaction is not needed.
Code Example
How this code works
This code's job is to open a continuous connection to a server and listen for real-time updates using Server-Sent Events (SSE). It sets up a client-side listener that can process different kinds of data streams as they arrive from the server, instantly updating a web page without needing constant requests.
The new EventSource('/stream-data') line initiates this persistent connection. Once established, eventSource.onmessage catches generic messages sent by the server, accessing the content via event.data to display it, perhaps updating an element like a live-feed. For more specific updates, eventSource.addEventListener('priceUpdate', ...) listens for custom-named events, allowing the code to handle different data types separately, like a stock price update. Crucially, eventSource.onerror handles connection problems. While EventSource usually tries to reconnect automatically, calling eventSource.close() inside the error handler explicitly stops further reconnection attempts, which is a subtle detail that can trip up beginners expecting the default auto-reconnect behavior.