---
title: "Server-Sent Events (SSE): Real-Time Without WebSockets"
description: "What Server-Sent Events are, how they differ from WebSockets and when to prefer them: an SSE server in Node.js, reconnection, Last-Event-ID, proxy settings and LLM streams."
url: https://sanderyaz.com/en/blog/server-sent-events-guide/
author: Sander Yazıcıoğlu
date: 2026-12-02
updated: 2026-12-02
lang: en
tags: ["Node.js","Real-time","Backend","Web"]
---

# Server-Sent Events (SSE): Real-Time Without WebSockets

At LiveTesting we used WebSockets for device control because client and server talked constantly in both directions. But many "real-time" needs are actually one-way: test run progress, notifications, showing an LLM's answer word by word. For those there's a simpler tool: **Server-Sent Events**.

## What is SSE?

A standard in which the server streams events to the client over a single HTTP response that stays open. Browsers expose it through the `EventSource` API.

| | SSE | WebSocket |
|---|---|---|
| Direction | Server to client | Both ways |
| Protocol | Plain HTTP | Separate protocol (upgrade) |
| Reconnection | Built in | You write it |
| Data | Text (UTF-8) | Text and binary |
| Proxy/CDN friendliness | Usually fine | May need extra config |

## An SSE server in Node.js

```ts
app.get('/runs/:id/events', (req, res) => {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    Connection: 'keep-alive'
  });

  const send = (event: string, data: unknown, id?: string) => {
    if (id) res.write(`id: ${id}\n`);
    res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
  };

  const unsubscribe = runEvents.subscribe(req.params.id, (e) => send(e.type, e.payload, e.seq));
  const heartbeat = setInterval(() => res.write(': ping\n\n'), 15000);

  req.on('close', () => { clearInterval(heartbeat); unsubscribe(); });
});
```

The `: ping` line is a comment; it isn't delivered as an event but stops proxies from closing the connection as idle.

## The client

```ts
const source = new EventSource(`/runs/${id}/events`);
source.addEventListener('step', (e) => render(JSON.parse(e.data)));
source.addEventListener('done', () => source.close());
source.onerror = () => showReconnecting();
```

## Drops and Last-Event-ID

When the connection drops, the browser reconnects automatically and sends the last `id` it received in a `Last-Event-ID` header. If the server replays events after that number, the user misses nothing. Keeping events briefly in a ring buffer or Redis Streams is enough.

## Things to watch

- **Connection limit on HTTP/1.1:** browsers open about 6 connections per domain; SSE across many tabs eats them. HTTP/2 or HTTP/3 removes the problem.
- **Proxy buffering:** Nginx needs `proxy_buffering off;` or an `X-Accel-Buffering: no` response header, or events arrive in batches.
- **Compression:** some compression middleware buffers the stream; exclude the SSE route.
- **Authentication:** `EventSource` can't send custom headers. Use cookie-based sessions or a stream read with `fetch`.

## Streaming LLM answers

Most LLM APIs already return token streams in SSE format. Keeping the same format when relaying from your backend to the browser is easy; because it needs POST, the browser uses `fetch` with a `ReadableStream` instead of `EventSource`. For LLM features in production, see [shipping LLM features to production](/en/blog/llm-features-in-production/).

## Which one when?

- The server talks, the client listens: **SSE**.
- Both sides talk often, or there's binary data: **WebSocket**. Details in [real-time systems with WebSockets](/en/blog/websocket-realtime-nodejs/).
- Data that changes every few minutes: plain polling may still be the simplest answer.

## Conclusion

For most real-time needs, SSE works with less code and fewer infrastructure problems than WebSockets. Picking the right tool avoids needless complexity from the start.

---

About the author: [Sander Yazıcıoğlu](https://sanderyaz.com/en/about/). Sander Yazıcıoğlu is a Senior Full-Stack Developer in Istanbul. At LiveTesting he builds the backend, frontend, realtime systems and Appium automation of a real-device testing platform; before that he built enterprise Angular apps at Testinium and Loadium.
