Master

Reading it as it arrives

I waited for the whole answer before showing any of it. A response can arrive in pieces and you render each piece as it lands. Streaming is a protocol, not a trick.

What you will see

one whole event per emit

About 1 min of reading

Try It

Consume a streamed response event by event.

1

Read the stream line by line. Each data event is parsed and shown as it arrives.

for line in resp.iter_lines(): if line.startswith(b'data:'): print(json.loads(line[5:]))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
{'token': 'Hel'} {'token': 'lo'}
2

A single event can be split across two byte chunks. Buffer before you parse, or you lose half an event.

buffer += chunk while b'\n\n' in buffer: event, buffer = buffer.split(b'\n\n', 1) emit(event)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
one whole event per emit
Do not parse each network chunk as if it were one event. A chunk boundary can land mid-event. Accumulate bytes and cut only on the blank-line boundary, then keep the leftover in the buffer.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

The server sends events as they are produced; you buffer bytes, split on the event boundary, and emit each event as it arrives, flushing at the end.
1 Server sends byte chunks
↓
2 Buffer and split on boundary
↓
3 Emit each event
↓
4 Flush partial at end
Why this works
Parse events, not bytes

A stream arrives in arbitrary byte chunks, so a line can be split across two. Buffer and split on the event boundary (the blank line), never assume one chunk is one event. That split is where streaming bugs hide.

Handle the end cleanly

The stream ends with a final marker or a close. Flush any partial buffer at the end and treat a broken pipe as a normal stop, not an error. A user stopping a request is not a crash.

Examples

Chunked transfer

Transfer-Encoding: chunked

The server does not know the total length, so it sends chunks with sizes. You get bytes before the end. The header says so.

Server-sent events

data: {"token": "Hel"}

data: {"token": "lo"}

Each event is a data: line, ended by a blank line. Concatenate the tokens and the answer appears word by word.

Consume the stream

for line in resp.iter_lines():
    if line.startswith(b'data:'):
        emit(json.loads(line[5:]))

iter_lines yields each event as it arrives. You act per event, never buffering the whole response.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The server calls you back
Previous Lesson
The server calls you back
Next Lesson
Build the small server
Build the small server →