Connection managers and broadcasting
Connection managers and broadcasting
A WebSocket handler knows about one socket: its own. The moment something outside that handler needs to send to it, whether a background worker that finished an escalation, a support agent joining the conversation, or an admin broadcast, you need a place where open sockets are kept and looked up. That place is a connection manager, and it is a small class: a dictionary from a key to a set of sockets, with connect, disconnect, and send_to methods.
The second problem arrives when you run two replicas. A socket lives in exactly one process, so a manager in process A cannot send to a socket held by process B. Redis pub/sub is the fix: every process subscribes to a channel, a send becomes a publish, and each process forwards the message to whichever sockets it holds.
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.