FastAPI websocket ping/pong timeout

Viewed 3473

I am using FastAPI with @app.websocket to listen for incoming websockets. How does FastAPI (or Starlette or Uvicorn underneath) do ping/pong heartbeats? Is this configurable? I cannot find it in the documentation at all.

from fastapi import FastAPI, WebSocket

app = FastAPI()

@app.websocket("/")
def ws(websocket: WebSocket):
    pass

fastapi uses starlette, and under the hood it seems to use websockets. websockets.connect by default uses a ping_interval and ping_timeout of 20 seconds, but I can't tell if that is used in FastAPI.

1 Answers

You can now use the 2 following flags:

  1. --ws-ping-interval <float> - Set the WebSockets ping interval, in seconds. Please note that this can be used only with the default websockets protocol.
  2. --ws-ping-timeout <float> - Set the WebSockets ping timeout, in seconds. Please note that this can be used only with the default websockets protocol.
Related