We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
You can continue the conversation there. Go to discussion →
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
When a WebSocket Ping message (data frame) is sent to a FastAPI WebSocket server, how can I receive it and return a Pong message?
Sending Ping using aiohttp as below, it doesn't seem that FastAPI WebSocket connection receives anything.
async with aiohttp.ClientSession() as sess: async with sess.ws_connect(WS_ENDPOINT) as ws: await ws.ping()
@app.websocket("/ws") async def websocket_endpoint(ws: WebSocket): await ws.accept() data = await ws.receive()
The text was updated successfully, but these errors were encountered:
imho the simplest is not to use FastAPI decorator but subclassing Starlette WebSocketEndpoint and sending your pong in the on_receive method
WebSocketEndpoint
on_receive
Oo that ping... 😄
Sorry, something went wrong.
Ping events are defined in the WebSocket spec: https://tools.ietf.org/html/rfc6455#section-5.5.2 and are handled automatically by Uvicorn, not by FastAPI.
@tiangolo Thank you for your response. I found the code that handles a Ping message in Uvicorn. https://github.com/encode/uvicorn/blob/master/uvicorn/protocols/websockets/wsproto_impl.py#L186-L187
No branches or pull requests
When a WebSocket Ping message (data frame) is sent to a FastAPI WebSocket server, how can I receive it and return a Pong message?
Sending Ping using aiohttp as below, it doesn't seem that FastAPI WebSocket connection receives anything.
Sending Ping
Receiving Ping
The text was updated successfully, but these errors were encountered: