Skip to content

Streaming responses in the UI

Showing a response while it is still arriving: read the body chunk by chunk, decode and re-frame the pieces, batch the paints, and keep a way to stop. A strong answer covers where the chunks break, not just the spinner.

On this page

What it is

Streaming means receiving a resource as a sequence of small chunks and handling each one as it lands, instead of waiting for the whole thing. In the UI, that turns a long wait and a sudden page of text into words that appear while the server is still working. This page covers three forms of it: a fetch response whose body is a ReadableStream, server-sent events delivered through EventSource, and streamed HTML where the server sends placeholder markup first and the real content later.

Reading a fetch stream. response.body is a ReadableStream. The spec says it is null when there is no body, though MDN notes that current browsers don't yet do that for real HTTP responses without one. Calling getReader() locks the stream to that reader; read() returns a promise for { done, value }, where value is a Uint8Array of raw bytes and is undefined once done is true. Browsers that support asynchronous iteration let you write for await (const chunk of response.body) instead. To stop early, pass an AbortSignal to fetch and call abort() on its controller.

Bytes to text, text to messages. The chunks are bytes, and a multi-byte character can be cut between two of them. TextDecoder.decode() takes a stream option: set it to true while more data will follow and false for the final call, and the decoder holds the incomplete bytes until the next chunk. Piping the body through a TextDecoderStream does the same job. Chunk boundaries don't line up with your messages either: MDN's line-by-line example keeps the text after the last newline and prepends it to the next decoded chunk, which is the pattern to reuse for any delimiter-framed stream.

ts
async function streamAnswer(prompt: string, signal: AbortSignal, onText: (t: string) => void) {
  const res = await fetch('/answer', { method: 'POST', body: JSON.stringify({ prompt }), signal });
  if (!res.ok || !res.body) throw new Error('request failed');

  const reader = res.body.getReader();
  const decoder = new TextDecoder();
  let tail = '';     // decoded text that is not yet a whole line
  let pending = '';  // tokens parsed but not yet painted
  let frame = 0;
  const flush = () => {
    frame = 0;
    if (pending) { onText(pending); pending = ''; }
  };

  while (true) {
    const { done, value } = await reader.read();
    // stream: true keeps a character split across chunks intact
    tail += decoder.decode(value, { stream: !done });
    if (done) tail += '\n'; // a last line may lack its newline
    const lines = tail.split('\n');
    tail = lines.pop() ?? ''; // the partial last line waits for the next chunk
    for (const line of lines) {
      if (!line.startsWith('data: ')) continue;
      const msg = JSON.parse(line.slice(6));
      if (msg.error) throw new Error(msg.error); // failure sent inside the stream
      pending += msg.token;
    }
    // paint at most once per frame, however many chunks landed
    if (pending && !frame) frame = requestAnimationFrame(flush);
    if (done) { flush(); return; }
  }
}

// The caller owns the controller and the partial text. On AbortError it keeps
// what arrived and marks it as stopped; on any other error it offers a retry.

Server-sent events. new EventSource(url) opens a connection to a URL that answers with the text/event-stream MIME type. Messages without an event name reach onmessage; a message with an event: field reaches the listener you add with addEventListener for that name. A message can carry a data field, and an id field that becomes the last event ID. If the connection drops, the browser restarts it by default, and close() ends it for good. The channel runs in one direction, server to client.

When to use it

Long generated text. Take a chat answer: the AI SDK 4 useChat hook requests the endpoint on submit and displays the reply as it streams, so the reader sees the first words before the response is complete. Its docs note that by default the hook renders on each incoming chunk, and offer experimental_throttle (the example uses 50 ms) to coalesce updates. Its status field moves through submitted, streaming, ready and error, which is a good shape for your own state: the spinner belongs to submitted, the Stop button to streaming. The server side of token streaming is on Streaming LLM tokens.

A page with one slow section. With streamed server rendering, React sends the HTML for everything outside a Suspense boundary first, sends the boundary's fallback in its place, and streams the real content when its data is ready. The Suspense reference, read for React 19.2, states that suspended content is revealed at most once every 300 ms from the last reveal, and that boundaries which become ready inside that window appear together, which cuts the pop-in of many small sections resolving one after another.

Screen readers. A screen reader attends to one part of the page at a time, so text growing elsewhere goes unnoticed unless it sits in a live region. Create the region empty before the first update, because assistive technology announces changes, not content that was there when it looked. Use aria-live="polite", which waits for a pause; assertive can clear whatever is being spoken. Our advice: announce the state changes (started, complete, stopped) and let the reader open the finished answer, rather than feeding tokens into the region.

Give the reader a way out. A Stop button that calls abort() on the controller ends the read loop. Hold one controller per request and abort the in-flight stream before starting the next one, the same discipline as Debounce and request cancellation. Keep the words that arrived rather than clearing them: the reader was reading them when they pressed Stop.

When not to

Short responses. When the whole body arrives in a fraction of a second, streaming adds a decoder and a framing buffer, and the reader sees no difference. Parse the complete body once and move on.

Nothing useful until the end. A total, a ranked list, a chart whose axes depend on the full data set: showing them half-built misleads more than a skeleton does. Fetch the whole result and show a placeholder sized like the final content.

The client needs to talk back mid-stream. Server-sent events flow in one direction, server to client, and the EventSource constructor takes a URL and a credentials option, so there is no request body to put a long prompt in. Read a fetch body when the request needs a body; reach for a WebSocket when both sides send during the conversation, as covered in WebSockets, server-sent events and polling.

Boundaries around fast data. A Suspense boundary around content that resolves quickly buys little: at best a brief fallback, at worst nothing the reader can see. React also shows the fallback again when already-visible content suspends anew, unless the update came through startTransition or useDeferredValue, so a boundary in the wrong place can blank a section the reader was looking at.

Compared

ApproachHow chunks reach youReach for it whenPick something else when

fetch + ReadableStream

Raw bytes from response.body; you decode and frame them

The request needs a body or custom framing, such as a chat prompt

A one-way feed on a URL is enough: EventSource does the parsing and reconnecting for you

Server-sent events

The browser parses text/event-stream into message events and restarts a dropped connection

One-way updates from a URL, with named event types

The request needs a body, or the client must send data back on the same connection

Streamed HTML with Suspense

Fallback markup first, real content later, revealed together inside a 300 ms window

A server-rendered page where one slow section should not hold the rest

The data is already fast: the boundary adds little the reader can see

Whole response

One body, read after the last byte

Short responses, or results that mean nothing until complete

The wait is long enough that a blank screen costs you the reader's attention

In a picture

fetch + signalServer

Step 1 of 4

Open the stream

The client sends the prompt with fetch, passing an AbortSignal. The server starts writing before it has finished, so the response body is a stream of byte chunks rather than one document.

How to say it in an interview

The common mistake

Quiz

A chunk ends halfway through a multi-byte character and the next chunk starts with the rest. What keeps it from rendering as a garbage symbol?
The client must send a long prompt and show the reply as it streams. Which transport fits, and why?
The reader presses Stop halfway through a streamed answer. What should the screen show next?
Two Suspense boundaries on a streamed page become ready 100 ms apart. What does React 19.2 do?

Related

Sources

  1. MDN: Using readable streams
  2. MDN: ReadableStreamDefaultReader: read() method
  3. MDN: Response: body property
  4. MDN: TextDecoder: decode() method
  5. MDN: TextDecoderStream() constructor
  6. MDN: Using server-sent events
  7. MDN: EventSource
  8. MDN: ARIA live regions
  9. MDN: ARIA: aria-live attribute
  10. React reference: <Suspense>
  11. React reference: renderToReadableStream
  12. web.dev: Streams, the definitive guide
  13. AI SDK 4 docs: Chatbot (Throttling UI updates)
  14. AI SDK 4 reference: useChat()
Sign in to report a mistake