
Engineering 9 min read· 22 Jul 2026· By Engineering
Streaming responses with SSE — a practical guide
Everything you need to know about server-sent events, chunk parsing, and cancellation.
Streaming turns a 4-second wait into a live typewriter. The mechanics are simple once you have seen the wire format.
The wire format#
text
data: {"choices":[{"delta":{"content":"Hello"}}]}
data: {"choices":[{"delta":{"content":" world"}}]}
data: [DONE]Client parsing#
javascript
const r = await fetch(url, { method: "POST", body });
for await (const chunk of r.body.pipeThrough(new TextDecoderStream())) {
for (const line of chunk.split("\n")) {
if (!line.startsWith("data: ")) continue;
const payload = line.slice(6);
if (payload === "[DONE]") return;
process(JSON.parse(payload));
}
}Cancellation#
Abort the fetch on user cancel — TokenBaazar stops billing at the last delivered chunk.
Never buffer the full response server-side just to forward it to the client. Pipe the stream through — that's the whole point.