Skip to content

What is SSE

Key idea:

SSE (Server-Sent Events, EventSource API) — a standard for streaming data from server to client over plain HTTP. Client opens a GET to the endpoint with Accept: text/event-stream, the server keeps the connection open and sends messages as they are ready. Unlike WebSocket: unidirectional (server→client only), works through any HTTP proxy, native auto-reconnect, no Upgrade handshake.

Below: details, example, related terms, FAQ.

Check your site →

Details

  • Content-Type: text/event-stream
  • Format: data: {json}\n\n
  • event: custom event name, id: for resume after disconnect
  • Reconnection: automatic with Last-Event-ID header
  • 6 connections per-domain limit in Chrome (HTTP/1.1). HTTP/2 removes the limit

Example

const es = new EventSource('/stream');
es.onmessage = e => console.log(JSON.parse(e.data));
es.addEventListener('user-login', e => {...});

Related Terms

How to Implement SSE in Your Web Application

Implementing Server-Sent Events (SSE) in your web application is straightforward. Below are the steps to set up a basic SSE server and client.

Server-Side Implementation

In a Node.js environment, you can create an SSE endpoint as follows:

const express = require('express');
const app = express();

app.get('/events', (req, res) => {
    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');

    setInterval(() => {
        const data = JSON.stringify({ message: 'Hello from server!' });
        res.write(`data: ${data}\n\n`);
    }, 1000);
});

app.listen(3000, () => {
    console.log('SSE server running on http://localhost:3000/events');
});

Client-Side Implementation

To receive events on the client side, use the EventSource API:

const eventSource = new EventSource('/events');

eventSource.onmessage = (event) => {
    const data = JSON.parse(event.data);
    console.log(data.message);
};

With this setup, your server will send messages to the client every second, demonstrating a basic implementation of SSE.

Advantages of Using SSE Over Other Technologies

Server-Sent Events (SSE) offer several advantages compared to other real-time data transmission technologies such as WebSockets and long polling.

  • Simplicity: SSE is simpler to implement than WebSockets. It uses plain HTTP, meaning you don't have to manage a separate protocol or perform an upgrade handshake.
  • Automatic Reconnection: SSE automatically handles reconnections if the connection drops, which reduces the complexity of error handling in your application.
  • Unidirectional Communication: Since SSE is designed for server-to-client communication, it avoids the overhead of bidirectional communication, making it a more efficient choice for scenarios where only the server needs to push updates.
  • Compatibility: SSE works seamlessly with existing HTTP infrastructure, including proxies and load balancers, ensuring that messages can be delivered without significant configuration changes.
  • Text-Based Streaming: SSE streams data as text, making it easy to read and debug compared to binary protocols.

These advantages make SSE an excellent choice for applications like live notifications, news feeds, and real-time updates where server-push functionality is required.

Common Use Cases for Server-Sent Events

Server-Sent Events (SSE) are particularly well-suited for a variety of real-time applications. Here are some common use cases:

  • Live Notifications: Applications that require real-time notifications, such as chat applications or social media updates, can leverage SSE to push notifications from the server to the client instantly.
  • Stock Price Updates: Financial applications can use SSE to stream live stock price changes to users, providing up-to-date information without requiring the client to poll the server.
  • Real-Time Analytics: Businesses can implement SSE to provide real-time analytics dashboards that display live metrics and KPIs, enabling better decision-making based on current data.
  • News Feeds: News websites can utilize SSE to push breaking news to users as soon as it becomes available, ensuring that readers receive the latest updates without refreshing the page.
  • Event Streaming: Applications that require continuous updates from the server, such as sports scores or live events, can benefit from the unidirectional stream provided by SSE.

These use cases demonstrate the versatility and effectiveness of SSE in delivering real-time data to users efficiently and reliably.

Learn more

Frequently Asked Questions

When to use SSE over WebSocket?

Server-only push (notifications, live feed). Simpler code, works through proxies. For chat/games you need WebSocket.

Automatic reconnect?

Yes. Browser reconnects with exponential backoff + sends <code>Last-Event-ID</code>.

HTTP/1.1 6-connection limit — a problem?

For a dashboard with 10+ SSE feeds — yes. Use a single multiplex endpoint or migrate to HTTP/2/3.

Try the live tool that powered this guide

Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.