Sub topic 18
When to Use WebSockets Instead of SSE
This chapter assumes you know how Server-Sent Events (SSE) carry LLM deltas over HTTP, and that you have used fetch and JSON. For most LLM chat apps, SSE is enough. This chapter covers the cases where it is not.
What SSE gives you and what it lacks
SSE is a one-way channel from server to client over a normal HTTP response. The client sends one request, usually a POST with the prompt, and the server streams events back until it finishes. Anything the client wants to say mid-stream needs a separate HTTP request.
A WebSocket is a long-lived, two-way connection. It starts as an HTTP request with an Upgrade: websocket header. After the server accepts, both sides send messages at any time, and each message is framed (text or binary).
| Property | SSE (over HTTP) | WebSocket |
|---|---|---|
| Direction | Server to client | Both directions |
| Data type | UTF-8 text | Text or binary |
| Reconnect | Built into `EventSource`, manual with `fetch` | You write it |
| Proxy and CDN support | Works like normal HTTP | Needs upgrade support |
| Message framing | Blank-line-separated events | Built-in frames |
| Auth headers in browser | Yes with `fetch` | Not on the browser `WebSocket` constructor |
Cases where WebSockets fit
- Voice and realtime audio. Audio chunks flow both ways at once: microphone input up, synthesized speech down. Binary frames avoid base64 overhead. Realtime model APIs commonly use WebSockets for this reason.
- Frequent client interruptions. If the user can barge in, send tool results, or change settings mid-generation, one open socket carries all of it. With SSE you would pair the stream with extra POST requests and match them by ID.
- Many short exchanges on one session. A socket avoids repeating connection setup and headers for each turn.
- Shared sessions. Several clients watch or steer the same conversation, and the server pushes updates to all of them.
Cases where SSE is still better
- A single prompt produces a single streamed answer.
- Cancellation is enough as the only client-to-server signal.
AbortControllercloses the connection, as covered in the cancellation chapter. - Your infrastructure is ordinary HTTP. Load balancers, caches, auth middleware, and logging already understand it.
WebSockets add costs. The server must hold state for every open connection. Deploys need to drain sockets. Load balancers need sticky routing or a shared session store. You also build your own heartbeats, reconnect logic, and message schema.
A minimal WebSocket client
The browser sends a prompt as a JSON message and receives delta messages on the same connection.
const socket = new WebSocket("wss://example.com/ws/chat");
socket.addEventListener("open", () => {
socket.send(JSON.stringify({ type: "prompt", text: "Hello" }));
});
socket.addEventListener("message", (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "delta") console.log(msg.text);
if (msg.type === "done") socket.close();
});
// Interrupt without opening a new HTTP request.
function stop() {
socket.send(JSON.stringify({ type: "cancel" }));
}Here cancel is a message your server must handle by stopping the upstream model call. Nothing cancels it automatically.
Exercise
A team builds a text chat where users type a question and read one streamed answer, with a Stop button. Should they use SSE or WebSockets, and why?
Show answer
SSE. The data flows one way after the request, and Stop can be done by aborting the fetch. WebSockets would add connection state, heartbeats, and proxy requirements with no benefit.
Exercise
Name two requirements that would justify switching to WebSockets.
Show answer
Any two of: two-way realtime audio, binary frames, the user interrupting or sending data mid-generation without extra requests, and several clients sharing one live session.