-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
✨ Export WebSocketState
in fastapi.websockets
#4376
Conversation
9fa01ba
to
87b8536
Compare
How do you use |
So there is an endpoint that is sending some data, usually very rarely but still, and is receiving nothing. So while we are not sending any data and the WebSocket connection was closed by the client, the task is still running. So to add functionality that if the connection is closed task will be closed also we are monitoring the WebSocket status, by simple The endpoint more or less looks like that: @router.websocket("/subscribe/some/data/changes")
async def subscribe_connectors_status(
websocket: WebSocket,
) -> None:
async def send_some_data() -> None:
last_data = await get_data()
await websocket.send_json({"data": last_data})
while True:
await asyncio.sleep(1)
new_data = await get_data()
if new_data != last_data:
# This happens very rarely that's why I would like to close all of these unused tasks.
last_data = new_data
await websocket.send_json({"foo": last_data})
async def watch_status() -> None:
while websocket.client_state == WebSocketState.CONNECTED:
# We are not expecting any data, but this will change to the client state if the connection will be closed.
await websocket.receive()
tg.cancel_scope.cancel()
await websocket.accept()
async with create_task_group() as tg:
tg.start_soon(watch_status)
tg.start_soon(send_some_data) |
Codecov Report
@@ Coverage Diff @@
## master #4376 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 535 535
Lines 13825 13826 +1
=========================================
+ Hits 13825 13826 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
📝 Docs preview for commit 36dadce at: https://630fd8992dbce202ce9f181a--fastapi.netlify.app |
WebSocketState
WebSocketState
in fastapi.websockets
Thanks for the contribution @matiuszka! 🍰 And thanks @Kludex for the help! 🙇 Knowing how you are using it made me feel it makes sense to have this in, so I'll take it. 🚀 Thanks! ☕ |
Trivial improvement about exposing
WebSocketState
. It will eliminate the need of importing it fromstarlette
directly.Previously:
After: