WebSockets in FastAPI
WebSockets in FastAPI
A WebSocket starts life as an HTTP request that asks to be upgraded, and once the server agrees, the connection stops being request and response and becomes two independent streams of messages. Either side can send at any time. That is the property server-sent events lacked: with a WebSocket, a Relay user can type a follow-up question, or press stop, while the model is still answering the previous one, and the server hears it on the same connection.
In FastAPI a WebSocket endpoint is an async def decorated with @app.websocket. It receives a WebSocket object, accepts the connection, and then usually loops: wait for a message, do something, send messages back, until the client goes away.
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.