07.02

Server-sent events for token streams

skillmaxingPython with AIServer-sent events for token streams
Visual lesson

Server-sent events for token streams

Included with Pro · 4:35
Lessons · 1,000 words

Server-sent events are the streaming format that browsers already know how to read. The server sends a plain HTTP response with Content-Type: text/event-stream and writes events as short text blocks; the browser's EventSource object turns each block into a JavaScript event, reconnects if the connection drops, and tells the server where it left off. For a token stream, which only ever flows from server to client, that is everything you need and nothing you do not.

Browser side, no code of yours
Figure 1Browser side, no code of yoursEventSource parses the frame, fires typed events, and reconnects on its own.

The format is so small that Relay does not need a library for it. It is JSON lines from the last lesson with a different frame: each event is one or more field: value lines followed by a blank line.

Pro

Continue reading

Pro unlocks every video lesson, the full notes and runnable code across the Python with AI course, from the language itself to agents, MCP, Langfuse and deployment.

Unlock full access to
Python with AIAI researchComplete system design
30% off with LAUNCH30ends in 7h 40m
Buy now
View all plans ·Already Pro? Sign in
124 online