What does it do?
Most communication between your browser and a website works like sending a letter: you ask a question, wait for a reply, and the conversation ends until you write again. The websockets library implements WebSockets, an approach that instead keeps a connection continuously open, like an actual phone call, so either side can speak up the instant something changes without waiting to be asked. This is what makes live chat messages, real-time notifications, and instantly updating dashboards possible without the page needing to constantly refresh. The library handles the technical handshake and message-framing details of that protocol so a Python program can just send and receive messages naturally.
See it in action
This starts a small server that keeps a connection open with a visitor and instantly repeats back whatever message it receives, without waiting to be asked again.
import asyncio
import websockets
async def echo(websocket):
async for message in websocket:
await websocket.send(message)
async def main():
async with websockets.serve(echo, "localhost", 8765):
await asyncio.Future()
asyncio.run(main())
Why would a non-developer care?
Anytime you’ve watched a chat message appear instantly, seen a stock price update live on a page, or watched a multiplayer game react in real time without refreshing, a technology like this was almost certainly involved. It’s the difference between an app that feels alive and one that feels like it’s constantly waiting for you to ask twice.
Real-world examples
WebSockets as a technology underpin real-time features across huge swaths of the modern web, from live sports scores to collaborative document editing to trading platforms, and the Python websockets library is one of the most widely used, actively maintained implementations of the protocol for Python developers. Without it, developers historically resorted to clunky workarounds like repeatedly asking the server for updates every few seconds, wasting bandwidth and adding delay.
Who uses it
Developers building real-time features like chat, live notifications, or collaborative tools where instant, two-way updates matter.
How it compares to alternatives
It implements the raw WebSocket protocol directly, while python-socketio builds a friendlier, more feature-rich layer on top of a similar idea, adding automatic reconnection and fallback options; developers who need the bare protocol reach for websockets, and those wanting convenience reach for Socket.IO instead.