websockets
WebSocket implementation for Python with RFC 6455 and 7692 compliance.
3. Used by agents' projects
Dependency in 125 public GitHub projects set up for AI coding agents that we scanned (2.0%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What websockets does
websockets is a Python library that does the WebSocket protocol and little else. It offers separate asyncio, threading, trio and Sans-I/O implementations, and its HTTP support stops at a health check.
websockets popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How websockets is used
- websockets is a dependency in 125 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (7.7% of the 1,623 Python ones), #7 of 126 in http clients and networking.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,473; those are left out of every share on this page.
- The most used alternative in our scan is websocket-client (18 projects).
- Projects that use it most often also use Uvicorn (72.0%), Pydantic (71.2%), FastAPI (66.4%).
- It is more common in projects set up for Roo Code (5.4%) than for OpenAI Codex (1.9%).
By coding agent
Often used with
websockets in practice
Strengths
The docs show an echo server and a client for each of <code>asyncio</code>, threading and <code>trio</code>, each in its own module. The threading examples are plain blocking code with no <code>await</code>. The docs call the trio version "functionally equivalent" to the asyncio one.
<code>websockets.sync.client.connect</code> opens a connection inside a <code>with</code> block, and you call <code>send</code> and <code>recv</code> directly. The docs call it "a good alternative for clients, especially if you aren’t familiar with asyncio."
The maintainers write that the library is heavily tested for compliance with RFC 6455. They also say continuous integration fails if branch coverage drops below 100%.
According to the docs, the library takes care of the opening and closing handshakes, pings and pongs, and other behavior in the WebSocket specification.
Watch out for
The README describes its HTTP support as "just enough for an HTTP health check." If you want HTTP and WebSocket in one server, it points to uvicorn or Sanic, which build on websockets.
The README says the library was "created to provide the best coroutine-based API." It tells anyone who wants callbacks to pick another library.
The original <code>websockets.legacy</code> implementation was deprecated in version 14.0. The docs say it will be removed by 2030. They tell existing users to upgrade to <code>websockets.asyncio</code>, which arrived in 13.0.
Echo server with the asyncio API
#!/usr/bin/env python
import asyncio
from websockets.asyncio.server import serve
async def echo(websocket):
async for message in websocket:
await websocket.send(message)
async def main():
server = await serve(echo, "localhost", 8765)
await server.serve_forever()
asyncio.run(main())More about websockets
Funding
- GitHub Sponsors: https://github.com/python-websockets
- Open Collective: https://opencollective.com/websockets
- Tidelift: https://tidelift.com/funding/github/pypi/websockets
- 2 GitHub sponsors
Packages around it
- requests: HTTP client
- python-dotenv: .env config loader
- solders: Solana types
- pydantic: data validation
- solana: Solana Python SDK
- anyio: async compatibility layer
Alternatives
websockets This page
WebSocket client library for Python with low-level API options.
Get started
Install it.
pip install websocketsRules for your agent
## websockets
- For asyncio code, import from `websockets.asyncio.server` and `websockets.asyncio.client`.
- If a project uses `websockets.legacy`, upgrade it to `websockets.asyncio`. The docs say legacy was deprecated in 14.0 and will be removed by 2030.
- For blocking code, use `websockets.sync.client.connect` inside a `with` block. Use `websockets.sync.server` only for servers that don't handle many client connections.
- In Trio projects, use the `websockets.trio.server` and `websockets.trio.client` modules.
- Don't implement handshakes, pings or pongs yourself. Call `send` and `recv`, or iterate with `async for message in websocket`.
- Don't add HTTP routes to a websockets server. Its HTTP support only covers a health check, so use uvicorn or Sanic when one server must serve both.
- Don't look for a callback API. The README says to pick another library if you need one.