Server-sent events for token streams
Server-sent events for token streams
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.
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.
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.