Node.js · Real-time · Backend · Web

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

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.

Sander Yazıcıoğlu Published: 2 min read Türkçe oku

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.

SSEWebSocket
DirectionServer to clientBoth ways
ProtocolPlain HTTPSeparate protocol (upgrade)
ReconnectionBuilt inYou write it
DataText (UTF-8)Text and binary
Proxy/CDN friendlinessUsually fineMay need extra config

An SSE server in Node.js

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

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.

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.
  • 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.

The Node.js Event Loop Explained with Examples

December 2, 2026 · 2 min read

The Node.js Event Loop Explained with Examples

The phases of the Node.js event loop, microtasks versus macrotasks, process.nextTick and setImmediate, and how to find code that blocks the loop.

Node.jsJavaScriptBackendPerformance

All articles →