Skip to content

Update ws example #117

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Update ws example #117

wants to merge 1 commit into from

Conversation

mattlink
Copy link

Adds another endpoint to examples/basic_ws.py which illustrates how to read data from the websocket.

It is not clear from the existing example (or docs, currently) that you can specify a data argument to the handler function to effectively get the contents of ws.receive(). There are other hidden arguments which seem to correspond to these annotations

@jph00
Copy link
Contributor

jph00 commented Jul 31, 2024

Thanks - this is a good idea. I guess it also probably needs something on the page demonstrating its use?

@jph00
Copy link
Contributor

jph00 commented Aug 11, 2024

@mattlink just bumping this...

@Kvit
Copy link

Kvit commented Feb 13, 2025

Another issue that I think should be covered in docs/examples is how to pass a variable from the web socket URL path. It is done via ws param.
Here is an example snippet

from starlette.websockets import WebSocket

@rt.ws('/wscon/{session_id}', conn=on_connect, disconn=on_disconnect)
async def ws_func(ws:WebSocket, data, send):
    session_id = ws.path_params.get("session_id")
    #... rest of the code

NB: you can pass URL path param to on_connect(), on_disconnect() functions as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants