Question presented to candidate: "We want a live order-status feed and a chat endpoint that shows an AI answer word by word. How would you stream from a Next.js Route Handler, and what would you check before it goes to production?"
What a strong answer should cover:
- Return a Web
Responsewhose body is aReadableStream; enqueue encoded chunks as they are produced and close the stream when done. - Server-Sent Events use
Content-Type: text/event-streamand frames likeid:,event:,data:ending in a blank line; the browserEventSourceparses them and reconnects automatically (honouringretry:). - Clean up on disconnect with
request.signaland the stream'scancel(); stop timers and upstream work. - Use
pull()for backpressure when wrapping a producer (an LLM, a file, a database cursor). - Production checks: proxies and CDNs that buffer (
X-Accel-Buffering: no,Cache-Control: no-cache, no-transform), the Node.js runtime and platform time limits, and sending a first byte early because headers go out with the first chunk.
Clarifying questions expected:
- "Is the traffic one-way server to client?" β SSE fits; two-way needs WebSockets or a separate service.
- "Where is it deployed, and what sits in front?" β serverless limits and buffering proxies decide feasibility.
Code / implementation expected: Yes β an SSE Route Handler with cleanup, and an EventSource client.