Streaming Interview Questions
9 curated streaming interview questions with detailed answers and examples.
← Browse all interview questionsThe server owns everything that must be trusted: the API key, the model choice, the system instructions, tool execution, auth, input validation, rate limits and saving the conversation. The client owns everything the user touches: the input box, the message list, rendering streamed parts, the stop button and loading or error states. In the App Router that usually means a Route Handler that returns a UI message stream and a Client Component that uses useChat.
Create an AbortController per request, pass controller.signal to fetch, and call controller.abort() from the Stop button or a useEffect cleanup. The fetch promise or the pending reader.read() then rejects with an AbortError, which you catch and treat as a normal stop, keeping the text already shown. On the server, pass the incoming request signal on to the upstream LLM call so the model actually stops generating and billing.
Start the upstream request, then hand its body stream straight to your response instead of awaiting text() or json(). In a Next.js Route Handler that means returning new Response(upstream.body); in Express, pipe Readable.fromWeb(upstream.body) into res. Then remove hidden buffers along the path (compression middleware, nginx proxy buffering, caches) and abort the upstream call when the client disconnects.
response.body is a ReadableStream of Uint8Array chunks. Get a reader with getReader(), call read() in a loop until done is true, and turn each chunk into text with a single TextDecoder using decode(chunk, { stream: true }). Then buffer the text and parse only complete lines or events, because network chunks do not line up with tokens or messages.
UTF-8 encodes many characters in 2 to 4 bytes, and the network can split a stream anywhere, including in the middle of one character. If you decode each chunk on its own, the incomplete bytes become the replacement character U+FFFD. Use one TextDecoder for the whole stream with decode(chunk, { stream: true }) (or TextDecoderStream), and flush it at the end, so incomplete bytes wait for the next chunk.
For a normal chat completion, Server-Sent Events (SSE) over HTTP is the better fit: the client sends one request, the server pushes tokens back in one direction, and it works with ordinary HTTP infrastructure, auth and serverless functions. The major LLM APIs stream over SSE for exactly this reason. WebSockets make sense when both sides need to keep sending data over one long-lived connection, such as realtime voice or a persistent agent session.
Keep finished messages stable and isolate the one that is still streaming, so only that bubble re-renders. Wrap message rows in React.memo with stable keys and unchanged object references, and batch incoming chunks so you update state at most once per animation frame instead of once per token.
Render markdown to React elements with a library like react-markdown, which escapes raw HTML by default, instead of piping parsed HTML into dangerouslySetInnerHTML. To avoid flicker and wasted work, split the text into blocks and memoize finished blocks so only the last one re-parses, and handle unclosed syntax such as an open code fence, either yourself or with a streaming-aware renderer like Streamdown.
Model each assistant reply as a message with a status (streaming, done, stopped or error) and a list of versions. Stop aborts the request and marks the current version as stopped while keeping its text. Regenerate rebuilds the history up to the user message before that reply, streams a new version into the same message slot, and keeps older versions available so nothing the user saw is silently lost.