FastAPI websockets let you push data to a client the moment it changes, without the overhead of polling. In a few lines you can expose a bidirectional channel, authenticate the user, and broadcast updates to every subscriber. Below I walk through a production-grade implementation: a simple endpoint, connection lifecycle, auth, a manager for broadcasting, and finally scaling with Uvicorn workers and Redis pub/sub.

How do I set up a basic WebSocket endpoint in FastAPI?

The first step is just a plain WebSocket route. FastAPI wraps Starlette’s WebSocket object, so you get async send/receive for free.

# app/main.py

from fastapi import FastAPI, WebSocket, WebSocketDisconnect