liveBook · Manning

WebSocket

As explained in Python Concurrency with asyncio

WebSocket is a network communication standard that provides a persistent, bidirectional connection between a client and server.

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

How Python Concurrency with asyncio covers it

How deep it goes
Explains persistent connections and implements endpoint callbacks, but does not descend into protocol internals.
Examples
Uses a live user counter, a Starlette endpoint, and a browser JavaScript client.
What it assumes
Contrasts the model with HTTP polling and uses asynchronous endpoint patterns.
What you can do after
Build it with Starlette to broadcast changing application data without repeated HTTP requests.
How it connects
Connects WebSockets with HTTP polling, ASGI, Starlette, asyncio, and REST APIs.

Also explained in

Compare how 2 books explain WebSocket →

Related concepts

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.