AI agents · OpenClaw · self-hosting · automation

Quick Answer

How to Stream LLM Responses: SSE vs WebSockets (2026)

Published:

The short answer

Stream with Server-Sent Events unless you need traffic in both directions during the response; then use WebSockets. SSE is plain HTTP, works through most proxies and CDNs, reconnects on its own, and matches how the model APIs stream to you, so your server can pass events straight through. WebSockets earn their extra complexity for voice, barge-in (interrupting the model), multiplayer and long agent sessions. Details below were checked against vendor documentation on November 22, 2026.

SSE vs WebSockets at a glance

Server-Sent Events (SSE)WebSockets
DirectionServer → client onlyBoth directions
ProtocolOrdinary HTTP response (text/event-stream)Upgraded connection (ws:// / wss://)
Browser APIEventSource (GET only) or fetch with a stream readerWebSocket
ReconnectAutomatic with EventSource, resumes via Last-Event-IDYou write it
Proxies and CDNsUsually fine; disable bufferingNeeds upgrade support
Model APIsOpenAI and Anthropic stream this wayOpenAI Responses API WebSocket mode
Best forChat answers, agent progress logsVoice, interruption, collaborative or multiplayer agents

Step 1 — Stream from the model API

Set stream: true. Anthropic’s Messages API emits typed server-sent events (message_start, content_block_delta with text, tool-use or thinking deltas, message_stop). OpenAI’s Responses API emits typed events such as response.output_text.delta and response.completed. Parse events by type rather than concatenating raw bytes, because tool calls and reasoning arrive as separate blocks.

Step 2 — Relay to the browser with SSE

Forward each text delta as an SSE event with an incrementing id: so a reconnecting client can say where it left off:

// Node.js route (Express/Hono style)
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("X-Accel-Buffering", "no"); // stop nginx-style buffering
let id = 0;
for await (const delta of modelStream) {
  res.write(`id: ${++id}\ndata: ${JSON.stringify(delta)}\n\n`);
}
res.write("event: done\ndata: {}\n\n");
res.end();

EventSource only sends GET requests, so many chat UIs use fetch() with a ReadableStream reader to POST the conversation and read the same event format. If you use the Vercel AI SDK or a similar library, it does this parsing for you.

Step 3 — Switch to WebSockets when the user talks back mid-stream

Typical triggers: a voice agent where the user interrupts, a stop button that must cancel tool execution server-side, several people watching one agent, or an agent session that lasts many minutes and receives new instructions. Keep one socket per session, tag every message with a type and turn ID, and reconnect on close — browsers do not do it for you. On Cloudflare Durable Objects, use the WebSocket Hibernation API so idle sockets do not bill compute time.

Step 4 — Survive production limits

  • Duration caps. Netlify Server closes WebSocket and SSE connections after 30 minutes (2 hours on Pro, Business and Enterprise) and replaces instances every 8 hours; an SSE stream also occupies one of an instance’s 32–128 concurrent request slots while it is open. Plan for reconnects everywhere.
  • Idle timeouts. Send a comment line (: ping) every 15–30 seconds during long tool calls so proxies do not drop a silent stream.
  • Buffering. Compression and reverse proxies can hold chunks; disable both on the stream route.
  • Resume, don’t restart. Store events per turn for a few minutes so a reconnect replays from Last-Event-ID instead of re-running the model (and paying twice).
  • Cancellation. When the client disconnects, abort the upstream model request; otherwise you pay for tokens nobody reads.

Step 5 — Measure what users feel

Log time to first token and time to last token per request from the client’s side. If the first token is slow, the cause is usually reasoning effort or a long prompt, not your transport — see how to reduce LLM latency. For hosting a backend that holds these connections, see where to host a long-running AI agent with WebSockets.

Last verified: November 22, 2026.

Sources