Under The Hood
backend fastapi FastAPI 0.100+ (Starlette WebSockets)

WebSocket Endpoint Handling, Connection Managers, and Broadcast Loops

Last updated
Prerequisites:
WebSocket protocol lifecycle
FastAPI dependency injection
  • fastapi
  • websockets
  • connection-manager
  • real-time
  • broadcast
  • chat

Read at your depth

The practical view

FastAPI provides native WebSocket support: `@app.websocket('/ws/{client_id}') async def ws_endpoint(websocket: WebSocket, client_id: str): await websocket.accept(); ...`. You read with `await websocket.receive_text()` and send with `await websocket.send_text()`. A `ConnectionManager` class tracks active `WebSocket` instances in a list/set and provides `broadcast(message)` to send data to all connected clients.

The same idea in other frameworks

fastapi equivalent
`@app.websocket('/ws')` with `await websocket.accept()`.

Legacy vs modern

Unmanaged Single WebSocket vs Production ConnectionManager

Raw WebSockets without cleanup cause memory leaks on disconnect; a ConnectionManager handles lifecycle and broadcasts.

before → after
Unmanaged WebSocket (Memory Leak)
while True: await ws.receive_text() // ❌ No disconnect handling, leaks on tab close
Production ConnectionManager Pattern
class ConnectionManager: ... try: ... except WebSocketDisconnect: manager.disconnect(ws) // ✅ NEW

Interview gotchas

Context

Interviewers test your knowledge of dependency injection across different ASGI protocol scopes.

The mechanical answer

Yes. FastAPI's `Depends()` works seamlessly in WebSocket endpoints (e.g. for extracting query parameters, cookies, or validating JWT tokens from query strings: `async def ws(websocket: WebSocket, token: str = Depends(get_token))`). If a dependency raises an `HTTPException`, FastAPI automatically closes the WebSocket with an appropriate close code.

Trap

HTTP Authorization headers cannot be passed in standard browser JavaScript `new WebSocket(url)` connections; pass auth tokens via cookies or URL query parameters instead.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.