An LLM answer can take several seconds to finish. If your UI shows a spinner for all of that time, the feature feels slow even when the model is fast. Streaming fixes the feeling: the first words appear almost immediately and the rest follow as they're generated.
The simplest transport for this is Server-Sent Events, or SSE. It is a one-way stream of text from server to browser over ordinary HTTP, with no WebSocket setup and no extra library. In this post I'll build both sides: a small Node.js server that streams tokens and cleans up after itself, a reader that parses the stream, and a React hook that ties it to the UI.
Why SSE and not WebSockets
Streaming a model answer is one-directional: the user sends a prompt once, and the server sends back many small pieces. SSE is built for exactly that. It travels over normal HTTP, works through most proxies, and its format is trivial to read. WebSockets are a better fit when both sides send messages continuously, such as a collaborative editor. For a chat-style answer, they are more machinery than you need.
The wire format in thirty seconds
An SSE response has the content type text/event-stream. It is plain text made of events separated by a blank line. Each event can have an event: name and one or more data: lines. A line starting with a colon is a comment, which is handy as a heartbeat. That is the entire format, and it is specified in the HTML standard.
event: token
data: "Hello "
event: done
data: {}
I send each token as a JSON string. That is not required, but it avoids a classic bug: a token containing a newline would otherwise break the framing, because a newline inside data: is meaningful.
The server
The server below uses only Node's http module. A fake model yields words with a small delay so you can see the behavior; replace it with your provider's streaming call. Four details matter more than the rest:
- Headers.
no-cachekeeps intermediaries from buffering, andX-Accel-Buffering: noasks nginx-style proxies not to hold the stream back. - Abort on disconnect. When the browser tab closes or the user presses stop, the
closeevent fires and we abort the generator. Otherwise you keep generating, and paying for, an answer nobody will read. - A heartbeat. A comment line every 15 seconds stops idle-connection timeouts from killing a slow response.
- A final
doneevent. The client needs a clear signal that the stream finished normally rather than being cut off.
server.js
import http from "node:http";
// Fake "model": yields a few tokens with a delay. Replace with your provider's streaming call.
async function* fakeModel(prompt, signal) {
for (const word of `You asked: ${prompt}. Here is a streamed answer.`.split(" ")) {
if (signal.aborted) return;
await new Promise((r) => setTimeout(r, 20));
yield word + " ";
}
}
export function createServer() {
return http.createServer(async (req, res) => {
const url = new URL(req.url, "http://localhost");
if (url.pathname !== "/stream") { res.writeHead(404).end(); return; }
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
Connection: "keep-alive",
"X-Accel-Buffering": "no", // ask nginx-style proxies not to buffer the stream
});
// Stop paying for tokens nobody will read.
const ac = new AbortController();
res.on("close", () => ac.abort());
const heartbeat = setInterval(() => res.write(": keep-alive\n\n"), 15000);
try {
for await (const token of fakeModel(url.searchParams.get("q") ?? "", ac.signal)) {
res.write(`event: token\ndata: ${JSON.stringify(token)}\n\n`); // JSON keeps newlines safe
}
res.write("event: done\ndata: {}\n\n");
} finally {
clearInterval(heartbeat);
res.end();
}
});
}
The reader
The browser has a built-in EventSource, and for simple cases it is great. Its limits are real, though: it only supports GET, and you can't set request headers, which means no Authorization header and no POST body for the prompt. For an authenticated chat endpoint, reading the stream with fetch is the practical choice.
This async generator reads the response body, splits on blank lines, ignores comment lines and yields { event, data } objects:
client.js
// Minimal SSE reader built on fetch(), so it also works with POST bodies and auth headers
// (the built-in EventSource only supports GET and cannot set headers).
export async function* readSSE(response, { signal } = {}) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let idx;
while ((idx = buffer.indexOf("\n\n")) !== -1) {
const block = buffer.slice(0, idx);
buffer = buffer.slice(idx + 2);
let event = "message";
const data = [];
for (const line of block.split("\n")) {
if (line.startsWith(":")) continue; // comment / heartbeat
if (line.startsWith("event:")) event = line.slice(6).trim();
else if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
}
if (data.length) yield { event, data: JSON.parse(data.join("\n")) };
}
}
} finally {
reader.cancel().catch(() => {});
}
}
Tests
I test the two sides against each other on a random local port. The first test checks the content type, collects the tokens, and asserts the final text and the done event. The second simulates a user pressing stop: it breaks out of the loop after two tokens, which cancels the reader and closes the connection.
sse.test.js
import test from "node:test";
import assert from "node:assert/strict";
import { createServer } from "./server.js";
import { readSSE } from "./client.js";
function listen() {
return new Promise((resolve) => {
const server = createServer();
server.listen(0, "127.0.0.1", () => resolve({ server, base: `http://127.0.0.1:${server.address().port}` }));
});
}
test("streams tokens then a done event", async () => {
const { server, base } = await listen();
const res = await fetch(`${base}/stream?q=hello`);
assert.match(res.headers.get("content-type"), /text\/event-stream/);
let text = "";
let sawDone = false;
for await (const msg of readSSE(res)) {
if (msg.event === "token") text += msg.data;
if (msg.event === "done") sawDone = true;
}
assert.equal(text.trim(), "You asked: hello. Here is a streamed answer.");
assert.ok(sawDone);
server.close();
});
test("client can stop early and the connection closes", async () => {
const { server, base } = await listen();
const res = await fetch(`${base}/stream?q=long`);
let n = 0;
for await (const msg of readSSE(res)) {
if (msg.event === "token" && ++n === 2) break; // user clicked "stop"
}
assert.equal(n, 2);
server.closeAllConnections();
server.close();
});
Both pass with node --test sse.test.js. Note what the second test does not do: it doesn't assert that the server's generator stopped. That would need a hook into the server, and it is the first thing I would add before relying on the cost-saving behavior in production.
The React hook
With the reader in place, the hook is short. It keeps the text in state, exposes a start and stop, and uses an AbortController so unmounting or pressing stop cancels the request. I haven't run this in a browser, so treat it as a starting point and test it in your own app.
import { useRef, useState } from "react";
import { readSSE } from "./client.js";
export function useStream(url: string) {
const [text, setText] = useState("");
const [running, setRunning] = useState(false);
const abort = useRef<AbortController | null>(null);
async function start(prompt: string) {
abort.current?.abort();
const ac = new AbortController();
abort.current = ac;
setText("");
setRunning(true);
try {
const res = await fetch(`${url}?q=${encodeURIComponent(prompt)}`, { signal: ac.signal });
for await (const msg of readSSE(res)) {
if (msg.event === "token") setText((t) => t + msg.data);
if (msg.event === "done") break;
}
} catch (e) {
if ((e as Error).name !== "AbortError") throw e;
} finally {
setRunning(false);
}
}
const stop = () => abort.current?.abort();
return { text, running, start, stop };
}
In a real app you would switch to POST with the prompt in the body, add the auth header, and render the text as plain text or sanitized Markdown, never as raw HTML.
Gotchas that bite in production
- Proxy buffering. Some reverse proxies and CDNs buffer responses and deliver the stream in one lump. If tokens arrive all at once in production but smoothly on your laptop, check proxy buffering and compression settings first.
- Platform timeouts. Serverless and container platforms cap how long a request can stay open. Check the limit for yours before promising long streams.
- Error handling mid-stream. Once you've sent a 200, you can't change the status. Send an
event: errorand let the client show a message. - Rendering cost. Updating React state for every token can re-render a lot. If the UI stutters, batch updates every 30 to 50 milliseconds.
- Moderation and validation. If you must check output before showing it, streaming raw tokens may not be acceptable. Decide that before you build the transport.
Testing the failure paths
The happy path is easy. Before release I'd also test what the user sees when the server dies halfway through a sentence, when the network drops on a train, and when the same user clicks the button twice. The hook above handles the last one by aborting the previous request before starting a new one. For the first two, the absence of a done event is your signal: if the stream ends without one, show the partial text with a "response was cut off, retry?" message instead of pretending it finished.
A short checklist
- Correct headers, and a heartbeat for slow answers.
- Abort the model call when the client disconnects.
- JSON-encode token data to protect the framing.
- Send an explicit
doneand, if needed,errorevent. - Test through your real proxy, not just on localhost.
Building a chat or AI assistant interface?
I build full-stack AI apps with React and Node.js, including streaming, auth and cost controls. If you want help getting yours production-ready, get in touch.
See services Get in touchWhat I ran this against
- Node.js 24.22.1 on 17 November 2026, using the built-in
node:testrunner. No provider API was called: the examples use stubs, so they cost nothing to run. Both tests pass: a full stream ending in a done event, and a client that stops early and closes the connection. - The server and the SSE reader were run against each other. The React hook below was type-checked by reading, but I did not run it in a browser. The model in the server is a fake token generator, so no provider was called.
Sources checked
- WHATWG HTML Standard, Server-sent events (the
text/event-streamformat:event:,data:, comment lines and blank-line separators): html.spec.whatwg.org. - MDN, Using server-sent events (the built-in
EventSourceAPI): developer.mozilla.org.