How do you handle a WebSocket client disconnect in FastAPI?

know FastAPI needs explicit cleanup for multiple WebSocket clients.
cite installing websockets, Handling disconnections and multiple clients pattern, and Depends.
saying FastAPI auto-cleans dead sockets with no cleanup.
WHAT THIS TESTS: This question tests whether a candidate knows that FastAPI WebSocket support requires installing the separate websockets library rather than being built in, and whether they are aware of the documented pattern for handling disconnections when multiple clients are connected. It also tests if they understand that dependencies can be used with WebSocket endpoints.
A GOOD ANSWER COVERS: A strong answer should start by stating that FastAPI relies on the external websockets package, which must be installed in a virtual environment according to the official docs. The candidate should then point to the documentation section specifically titled Handling disconnections and multiple clients as the authoritative guide for cleanup logic and removing a client from an active connection list. They should mention that the docs cover awaiting messages and sending messages, and that the disconnection pattern is the canonical reference for resource cleanup. The answer should also note that FastAPI WebSocket endpoints support dependencies using Depends and others, meaning authentication or shared logic can be injected just like in HTTP routes. Finally, the candidate should acknowledge that in production the frontend is typically built with a modern framework such as React, Vue.js, or Angular, so the disconnect handling must align with how those clients close connections.
COMMON WRONG ANSWERS: A red flag is claiming that FastAPI automatically detects when a client drops and cleans up resources without any explicit code. Another red flag is omitting the websockets dependency entirely or treating WebSockets as built into FastAPI without installation. Candidates should not ignore the multiple clients aspect; an answer that only discusses a single socket without referencing the documented pattern for tracking many connections is incomplete. Also, suggesting that dependencies cannot be used with WebSockets is incorrect because the docs explicitly cover using Depends and others in this context.
LIKELY FOLLOW-UPS: An interviewer might ask how you would test WebSocket disconnections, which ties into the Testing WebSockets section of the docs. They might also ask about lifespan events, since those are adjacent to connection management, or how to inject shared state via dependencies rather than global variables.
ONE CONCRETE EXAMPLE: Suppose you are building a chat endpoint. A senior candidate would explain that after installing websockets, they would create a websocket path operation, then reference the Handling disconnections and multiple clients documentation to implement the removal of the socket from the active set when the frontend built in React or Vue closes the connection. They would also mention that Depends could be used to inject a shared connection registry.
Read the original → fastapi.tiangolo.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.