Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket closed by client -> starlette.websockets.WebSocketDisconnect #1799

Closed
Yaakov-Belch opened this issue Jul 27, 2020 · 2 comments
Closed

Comments

@Yaakov-Belch
Copy link

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.

@tiangolo
Copy link
Owner

tiangolo commented Aug 9, 2020

@rkbeatss added docs for this in #1822, here: https://fastapi.tiangolo.com/advanced/websockets/#handling-disconnections-and-multiple-clients 🎉

@Yaakov-Belch
Copy link
Author

Thanks a lot. I am happy and close this issue.

@tiangolo tiangolo changed the title [DOCUMENTATION]: WebSocket closed by client -> starlette.websockets.WebSocketDisconnect : WebSocket closed by client -> starlette.websockets.WebSocketDisconnect Feb 24, 2023
@tiangolo tiangolo changed the title : WebSocket closed by client -> starlette.websockets.WebSocketDisconnect WebSocket closed by client -> starlette.websockets.WebSocketDisconnect Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7388 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants