-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Description
The problem
The documentation at https://fastapi.tiangolo.com/advanced/websockets/ fails to explain what happens
when the client closes the WebSocket connection. This information is needed because real APIs need to
handle this case.
Note: The behavior is documented in the starlette documentation: https://fastapi.tiangolo.com/advanced/websockets/ --- but the documentation is not exact: It misspelled the Exception class name (I will add a separate documentation issue with starlette regarding this inaccuracy).
The solution
Add the following text to the documentation: "When the client closes the WebSocket connection, the call await websocket.receive_text() will raise an starlette.websockets.WebSocketDisconnect exception. You can catch this exception to handle this condition."
In addition, we may want to adjust the sample code to show this in action.
In addition, you may want to re-export that exception class via fastapi like you do with many starlette features that are used idiomatically in fastapi.
Help
I'll be happy to create a documentation pull request --- if desired. I have extended the chat API application to share all chat messages to all connected clients (this requires to remove disconnected clients from the set of clients). If you like, I can add this to the documentation for a slightly more complete example.