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's really being asked
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.
The full answer
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.
The mistakes people make
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.
What usually comes next
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.
A 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.
Interview question
When implementing a FastAPI WebSocket endpoint that tracks multiple active clients, which practice aligns with the official documentation?
- a.Install the websockets library and follow the documented pattern to explicitly remove disconnected clients from the active connection setCorrect
- b.Rely on FastAPI's built-in WebSocket support to automatically remove clients when connections drop
- c.Avoid using Depends in WebSocket handlers because dependency injection is only supported for HTTP routes
- d.Reference the Testing WebSockets documentation as the primary guide for connection cleanup logic
Why? this is the answer
The official docs require installing the external websockets package and using the explicit Handling disconnections and multiple clients pattern for cleanup. Distractor A is tempting because frameworks often abstract connection management, but FastAPI does not auto-clean dead sockets, and D misdirects to the wrong documentation section.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles