SSE Streaming in 20 Lines: The 7 Misconceptions That Separate Copying From Comprehension
SSE streaming breaks in ways that only surface under real conditions, and the misconceptions above are exactly where the breakages happen: a chunk splits a multi-byte character, a disconnect drops mid-stream, the buffer silently corrupts output. Knowing which flag controls what and where the loop really exits determines whether those failures take minutes or hours to debug.
SSE streaming from a Vue3 frontend works as a chain: the request body carries stream: true, the server pushes partial text as it generates, reader.read() pulls chunks one at a time, TextDecoder converts bytes, buffer holds incomplete data, split('\n') cuts full lines, and JSON.parse extracts choices[0].delta.content, which accumulates into output.value and triggers a Vue re-render. That re-render is the literal cause of the typewriter effect.
The entire mechanism exists to handle two facts. Network chunks never arrive aligned with SSE's requirement that each message occupy one complete line ending in a newline, and the server sends only incremental new characters per event, never the whole answer. Buffer exists as a temporary pool that catches the trailing half-line after split, and pop() is the only correct retrieval because the incomplete segment always sits at the tail. shift() would grab complete lines from the front and cost O(n) array movement.
Seven pitfalls read as a confession list. [DONE] gets skipped as a content line while the loop actually exits on reader.read() resolving done: true. The while loop wraps the whole cycle; it isn't the output step. The two stream: true flags do unrelated jobs, one telling the server to push and the other telling TextDecoder to stitch multi-byte characters split across chunks, a difference that breaking Chinese or emoji output exposes. Disconnects throw a Promise rejection rather than a done flag, and already-rendered text survives them. A four-layer verification method closes things out: retell, explain why, adapt to a new scenario, and predict output before running.
Shared identifiers across API layers make shallow reading actively misleading: two stream: true flags do unrelated work, yet the identical name guarantees developers conflate them until a multi-byte corruption forces the issue.
The [DONE] mix-up generalizes to streaming APIs beyond this stack: protocol sentinels are conventions the tools define, but the real exit condition lives in the transport layer, and developers who conflate them misdiagnose stream termination.
Output prediction is a stronger comprehension check than any written test: changing one input, forecasting the rendered result, and comparing against the run isolates mechanism gaps that explanations conceal.
Misconceptions surface only at failure time: the seven items above all appear on disconnects, multi-byte boundaries, and protocol edge cases that tutorials don't exercise, so unexamined streaming code works precisely until it breaks.
Too right, the part about the two stream:true is the key. That skin-deep pretense gets torn off harshly.