WebSocket replaces repeated HTTP polling with one persistent, bidirectional connection. The server can push updated data when it changes, avoiding requests that return nothing new. section 9.3.2
A practical WebSocket endpoint follows this lifecycle:
- Accept a connection and add its socket to an in-memory client list. section 9.3.2
- Send the current user count to connected clients whenever someone connects or disconnects. section 9.3.2
- Remove disconnected clients and handle send failures so stale sockets do not remain. section 9.3.2
Live user count
A browser opens a WebSocket to a Starlette endpoint and updates the page from each incoming message. The server broadcasts the number of connected users without waiting for browser polls. section 9.3.2

