top of page

SSE (Server-Sent Events)

Architectural Style & Patterns: Server-Sent Events (SSE), sometimes known by its API name EventSource, is an HTTP-based protocol that enables a server to push text data to clients in real-time. It establishes a uni-directional channel over HTTP: the client opens a connection (an HTTP GET request) which the server keeps open and continuously sends events as a stream. SSE is built on top of standard HTTP, typically using the text/event-stream content type. Importantly, SSE is one-way – from server to client. Clients can’t send data back on the same connection; any client-to-server communication must use another channel (like a separate REST call). SSE is client-server in nature and aligns with an event-driven push model (server push, client reactive). It’s simpler than WebSockets (no custom framing, just a continual HTTP response) and is well-supported in browsers via the EventSource API. SSE is automatically reconnection-capable – if the connection breaks, the client (EventSource) will retry after some seconds, and the server can even send a special retry interval or last-event ID to avoid missing events. SSE is useful in web applications for real-time updates where the data flows only from server to client (e.g., notifications, feeds, live data displays). It can be considered an easier-to-use alternative to WebSockets when you don’t need two-way communication. SSE fits microservices if used in a gateway scenario – e.g., a service can aggregate events and push to web clients. But more often, SSE is at the edge (server to browser/app). Because it’s HTTP-based, it works with existing infrastructure (proxies, load balancers) without special requirements. SSE is less popular than WebSockets for flashy real-time apps, but it has a niche for streaming events/updates like live scores, ticker feeds, or incremental results from a long process. It’s worth noting SSE sends events as text (usually encoded as UTF-8 text lines), not binary. It’s typically not used for huge volumes of data, but rather continuous small messages.



Server-Sent Events (SSE) showing a one-way communication flow from a server to a browser client over a single HTTP connection. The image highlights real-time updates, such as live news feeds or stock prices, being pushed from server to client using a lightweight, event-stream format.

Strengths and Use Cases: SSE has some clear advantages in the scenarios it targets:


  • Simplicity and Ease of Use: Using SSE in a browser is as simple as creating a new EventSource(url). The browser handles opening the HTTP connection and reconnecting if needed. The server just writes to an HTTP response stream. There’s no complex handshake or protocol upgrade as with WebSockets. This simplicity makes SSE a low-friction way to push updates. It’s ideal for quickly adding live notifications or updates to a web page without pulling in heavier libraries or protocols.


  • Leverages HTTP Infrastructure: SSE being an HTTP long-lived response means it usually plays fine with existing proxies and network setups. Many corporate networks that might block WebSockets will still allow HTTP. SSE also can be easier to secure (you can use standard HTTPS, and even reuse things like cookie-based auth or headers since it’s just an HTTP request). Logging and monitoring tools that understand HTTP can also track SSE connections. So, when compatibility is a concern, SSE can be more reliable than WebSockets – for instance, some old proxies don’t handle WebSocket upgrades well but will happily forward an HTTP stream.


  • Automatic Reconnection & Event ID: The EventSource API automatically tries to reconnect if the connection drops, and it can send a special Last-Event-ID header to the server on reconnect (if the server was sending event IDs). This means it’s easy to make the stream resilient to brief outages or server restarts – the client can resume and the server can potentially replay missed events based on last ID. This built-in resilience is a big plus over manually coding reconnection logic for WebSockets or polling. It ensures that clients in poor network conditions can still get most of the data eventually.


  • Ordered, Simple Message Format: Events are delivered in the order the server sends them (over a single connection). The text/event-stream format is simple: lines of text, with event and data fields. It’s easy to produce on the server (just print text lines) and easy to parse on the client (the browser API does it). This is great for broadcasting chronological events like news feeds, logs, or progress updates. Each message can be tagged with an event type so you can have multiple event streams multiplexed (e.g., “message” events vs “notification” events) without designing a complex protocol.


  • Lightweight for Server Push: SSE is very lightweight in terms of implementation. Many web frameworks support writing chunked responses which is essentially what SSE is. It doesn’t require threads per connection (most servers handle it in an async/evented way, similar to WebSockets). If you only need server→client push, SSE avoids the overhead of a full WebSocket. It’s also one-way, meaning the server is in control of message flow (which can be good if you want to strictly manage what clients can do – they can’t spam the server with data on the SSE connection).


  • Use Cases:

    • Live Feeds and Notifications: Perfect for things like social media feed updates, live blog comment updates, news tickers, stock price updates (if only pushing from server).

    • Monitoring and Dashboards: Streaming metrics or logs from a server to an admin dashboard can be done via SSE. E.g., tailing a log file in a web UI can be implemented with SSE.

    • Progress Updates for Long Tasks: If a client triggers a long-running process (like a video rendering or data export), the server can stream periodic progress events via SSE to the client so the UI can update a progress bar in real-time.

    • Chat or Multi-user collaborative events (server to clients): For instance, in a chat room, you could use SSE to push new messages to all participants (but note: for the user sending a message, you’d still need a POST or some way to send to server, since SSE can’t carry client-to-server).


Weaknesses and Limitations: SSE is not a panacea and has notable constraints:


  • One-Way Communication: The biggest limitation is that SSE only goes from server to client. If the client needs to send data frequently (e.g., in a game or collaborative app where both sides send streams of events), SSE alone is insufficient. You’d have to supplement it with something else (like the client using fetch/AJAX for sends, which is fine for low-frequency interactions but not real-time). In such fully interactive cases, WebSockets or WebRTC data channels are better.


  • Concurrent Connection Limits: SSE uses an HTTP connection. Browsers typically limit the number of concurrent connections to the same domain (commonly 6). An EventSource takes up one of those, constantly. This is usually fine, but if a page is already making many requests or has multiple EventSources, you could hit limits. WebSockets, once established, don’t count against that limit in the same way because it’s an upgraded connection. So SSE could theoretically throttle other HTTP interactions if abused.


  • Message Size and Binary Data: SSE is text-based. While you can send things like base64-encoded binary, it’s not efficient for large binary data. If you needed to stream, say, raw audio or a binary protocol, SSE isn’t suitable. Also, each SSE message is typically expected to be relatively small (since it’s often just one line of text data); very large messages might be better handled via other means (or split).


  • Server Resource Usage: Each SSE connection is essentially an open HTTP request. Traditional web servers might allocate resources per open request (thread or process in some cases), meaning serving lots of SSE clients could be heavy if not using an async server. Modern servers/frameworks mitigate this (using non-blocking I/O), but it’s a consideration. In high fan-out scenarios (thousands of clients), one needs to ensure the server handles long-lived connections efficiently.


  • Lack of Fan-out Support at Network Level: Unlike MQTT or AMQP where the broker can do one-to-many distribution, SSE is one stream per client. If you have 1000 clients and you want to send the same event to all, you have to write it 1000 times (once to each connection). This can be a lot of outgoing bandwidth and processing if the audience is large. WebSockets have the same issue. Technologies like multicast or specialized event brokers could optimize broadcast, but SSE itself has no built-in notion of multicasting events to many clients with one send (that would be more the job of an intermediate push service).


  • No Built-in Retries for Server Failures beyond Last-Event-ID: SSE auto-reconnects on network issues, but if a server goes down and loses state of events, the client may miss events unless the server app is built to handle that via event IDs. Implementing reliable “catch-up” on reconnect might require storing recent events on the server. SSE doesn’t solve that beyond providing the hook (last event ID); the logic is up to you.


  • Not Suitable for High-Frequency Low-Latency Interactions: If you need millisecond-level latency or 50 updates per second (like a multiplayer game), SSE (over HTTP/1 especially) might not keep up as nicely as a raw socket. HTTP/2 can help multiplex SSE too, but at some point WebSockets or UDP-based protocols become more appropriate.


  • Browser Support (Older IE): SSE is broadly supported in modern browsers (Chrome, Firefox, Safari, Edge). It wasn’t supported in IE10 and below, so historically that was a limitation. But in 2025, that’s hardly relevant except for extremely legacy environments.


In practice, SSE is best used when you need a straightforward server→client push and do not need the client to push data on the same channel. It’s perfect for scenarios like real-time dashboards, notifications, or incremental data feeds to users. It’s also a good choice in environments where WebSockets are problematic (corporate networks) because it’s just HTTP. Developers often choose SSE over WebSockets when they want to avoid the overhead of managing a full two-way socket or when the communication is largely one-directional anyway. If two-way is needed, many frameworks might use WebSockets (or a library like Socket.IO which can fall back to SSE/long-polling if needed). SSE, being text-based and one-way, is a niche but useful tool especially for event-driven UI updates in web applications where the web server or an API wants to continuously send data to the front-end.


Main Blog:

Commentaires


bottom of page