A WebSocket endpoint is a server-side interface that manages one persistent, bidirectional connection. Unlike HTTP polling, it lets the server push new data when it becomes available instead of waiting for repeated requests. section 9.3.2
A Starlette WebSocket endpoint typically handles the connection through lifecycle coroutines:
- on_connect accepts the socket, stores it, and broadcasts the current connection count. section 9.3.2
- on_receive handles client messages; it can remain empty when clients send no data. section 9.3.2
- on_disconnect removes the socket and updates the remaining clients, with failed sends triggering removal. section 9.3.2
Live user counter
A UserCounter subclass keeps an in-memory list of connected WebSockets. Each connection or disconnection recalculates the count and sends it to connected clients; browser JavaScript displays the value.
