# Streaming with fetch: LLM-style Responses — WebSockets & Server-Sent Events

Source: https://www.skillbyai.com/en/realtime-web/rt-sse-fetch-streaming

> Consume an SSE-formatted stream with fetch and ReadableStream when EventSource is not enough.

## When EventSource is not enough

EventSource only sends GET requests and cannot set headers like `Authorization`. LLM chat APIs need a POST body (the prompt and settings) and an API key, so their clients read the stream with **`fetch`** instead. `response.body` is a `ReadableStream` of bytes. Piping it through `TextDecoderStream` gives text chunks, but chunk boundaries are arbitrary: one chunk may hold half an event or three events. The client therefore keeps a **buffer**, splits on the blank-line separator `\n\n`, processes complete events and keeps the incomplete tail for the next chunk. Many APIs end with a sentinel such as `data: [DONE]`. The trade-off: with fetch you lose automatic reconnection and `Last-Event-ID` handling, so you write them yourself, usually by retrying the request or resuming from a saved position. To cancel a long answer, pass an `AbortController` signal to `fetch` and call `abort()`.

## Assembling tokens from a streamed POST

The server writes one token per event. The client buffers chunks, splits on blank lines and joins the tokens. The same loop works against real LLM APIs that use SSE framing.

```js
import http from 'node:http';

const server = http.createServer(async (req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/event-stream' });
  for (const token of ['Real', '-time', ' is', ' fun']) {
    res.write(`data: ${JSON.stringify({ token })}\n\n`);
    await new Promise((r) => setTimeout(r, 50));
  }
  res.end('data: [DONE]\n\n');
});

server.listen(3002, async () => {
  const resp = await fetch('http://localhost:3002/chat', { method: 'POST' });
  const reader = resp.body.pipeThrough(new TextDecoderStream()).getReader();
  let buffer = '';
  let answer = '';
  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    buffer += value;
    const events = buffer.split('\n\n');
    buffer = events.pop(); // keep any half-received event for the next chunk
    for (const evt of events) {
      const data = evt.replace(/^data: /, '');
      if (data === '[DONE]') continue;
      answer += JSON.parse(data).token;
    }
  }
  console.log(answer);
  server.close();
});
```

Output:

```
Real-time is fun
```

## Never assume one chunk equals one event

Code that does `JSON.parse(chunk.slice(6))` works on localhost and fails in production, where TCP and proxies merge or split chunks. Always buffer and split on the event separator.

## Reading a WhatsApp forward in parts

A long message that arrives in pieces cannot be read sentence by sentence as each piece lands; sometimes a piece ends mid-word. You keep the unfinished part in mind and complete it when the next piece arrives. The `buffer` variable is that unfinished part.

**Quiz:** Why do LLM chat clients usually read streams with fetch instead of EventSource?

- [ ] fetch is the only API that supports UTF-8
- [ ] EventSource cannot parse JSON
- [x] They need a POST body and custom headers such as Authorization
- [ ] EventSource cannot stream more than 100 events

*Answer:* They need a POST body and custom headers such as Authorization. EventSource is GET-only and cannot set custom headers. fetch can send a POST with an API key and still read the response as a stream.
