DocsBuild
WebSocket handlers
Handle WebSocket connections in a .py route file by defining SOCKET.
from fastapi import WebSocket
async def SOCKET(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("hello")
await websocket.close()Rules
- The URL is the file path, same rule as routing.
- Only
.pyroute files register WebSocket handlers viaSOCKET. - Make sure your reverse proxy forwards WebSocket upgrade requests.
Next steps
- API: HTTP handlers in the same files
- Deploy: proxy configuration
- Modular routing:
SOCKETon a controller