liveBook · Manning

Websocket endpoint

As explained in Python Concurrency with asyncio

A WebSocket endpoint is a server-side interface that accepts and manages a WebSocket connection.

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.

How Python Concurrency with asyncio covers it

How deep it goes
Explains lifecycle hooks, connection tracking, broadcasting, and basic send-failure cleanup.
Examples
Uses a Starlette user counter with an in-memory socket list and a browser display.
What it assumes
Assumes basic web application knowledge while introducing persistent sockets and their lifecycle from the polling contrast.
What you can do after
Build it from scratch using Starlette lifecycle coroutines and a connected-client collection.
How it connects
Connects WebSockets with HTTP polling, Starlette, asynchronous coroutines, and bidirectional communication.

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.