SSE stands for Server-Sent Events, a web standard that allows servers to push real-time updates to clients over HTTP. It enables live data transmission without requiring constant polling.
SSE operates on a unidirectional channel: the server sends data to the client, typically a web browser. The client maintains an open connection and listens for incoming messages. This model suits scenarios like live sports scores, stock tickers, or system status alerts where updates flow from server to user.
Unlike WebSocket, which supports bidirectional communication, SSE is designed for one-way data streams. It uses standard HTTP, making it easier to implement through firewalls and proxies. The protocol runs over HTTP/1.1 and can be extended using custom event types and data formats.
The client establishes a persistent connection to the server using JavaScript's EventSource API. Once connected, the server sends data as plain text with a specific format:
data: New message from server
data: Another line
data: Last line
event: update
data: This is a custom event
Each line begins with a prefix: data: for message content, event: for custom event names, and id: to track message sequences. Empty lines terminate a message block.
When the server closes the connection, the client automatically attempts to reconnect unless explicitly disabled. Reconnection intervals are configurable via the retry: directive in the response header.
For example, a news feed server might send:
data: Breaking: Major earthquake in California
event: alert
id: 12345
The browser receives this as a DOM event named "alert" with data containing the breaking news.
SSE supports multiple message types and can be adapted for different use cases. The most common variants are:
| Type | Description | Use Case Example |
|---|---|---|
| Default data | Messages sent with data: prefix |
Live weather updates |
| Custom events | Defined with event: header |
System warnings or user actions |
| Reconnection control | Set via retry: header |
Automatic retry after network failure |
| Message ID tracking | Identified with id: field |
Resuming stream after disconnection |
Server-side implementations vary by language. Node.js uses the `EventSource` module; Python employs `flask-sse`; Go implements it via `net/http` with chunked encoding. All rely on HTTP chunked transfer encoding to stream data incrementally.
Modern frameworks like Express.js and Django support SSE through middleware. A basic Express route might look like:
app.get('/stream', (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
const interval = setInterval(() => {
res.write(`data: ${new Date().toLocaleTimeString()}\n\n`);
}, 1000);
req.on('close', () => clearInterval(interval));
});
This sends a timestamp every second until the client disconnects.
SSE is ideal for applications needing low-latency, one-way data delivery. Common uses include:
Twitter's original timeline used SSE to deliver new tweets. The protocol reduced latency compared to polling and lowered server load. According to Mozilla Developer Network, SSE reduces bandwidth usage by up to 60% in high-frequency update scenarios.
Facebook Messenger uses SSE for presence indicators (online/offline status). When a user logs in, the server pushes a status update to all connected clients instantly. This avoids the delay inherent in periodic polling.
A key advantage is reliability. If the connection drops, the client retries automatically. The server can resume sending from the last known id: value, ensuring no data loss during brief outages.
SSE is often confused with WebSocket, but they serve different purposes. WebSocket enables two-way communication and is better suited for chat applications or multiplayer games. SSE is simpler, uses standard HTTP, and doesn't require a separate protocol layer.
Another myth is that SSE requires long-lived connections to be inefficient. In reality, HTTP/1.1 keeps connections alive by default. Modern servers like Nginx and Apache handle thousands of concurrent SSE connections efficiently. A 2021 benchmark by Cloudflare showed that a single Nginx instance handled 12,000 concurrent SSE streams with under 50ms latency.
Some developers assume SSE only works with JavaScript. While the primary interface is JavaScript's EventSource, the protocol is language-agnostic. REST APIs, mobile apps, and IoT devices can consume SSE streams using standard HTTP clients.
I once deployed an internal monitoring dashboard using SSE to track server CPU usage. Over 300 users accessed the feed simultaneously. The server sustained 150 requests per second with minimal memory overhead. The system ran for six months without downtime.
The W3C published the Server-Sent Events specification in 2015. It defines the protocol syntax, error handling, and connection lifecycle. The specification is available at W3C EventSource Recommendation.
MDN Web Docs provides detailed implementation guidance. They confirm that SSE is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. Support is enabled by default and does not require polyfills.
According to the W3C, SSE is designed for use in environments where security is managed via HTTPS and CORS policies. Servers must set the correct headers: Content-Type: text/event-stream and Cache-Control: no-cache.
SSE stands for Server-Sent Events, a lightweight, standardized way for servers to push real-time data to clients. It's efficient, secure, and widely supported. Use it when you need live updates without the complexity of full-duplex protocols.