This release is a pre-release and may not be stable for production use.
sillo-wire
Rooms, presence and fan-out for Sillo WebSockets.
pip install sillo-wire
Installs as sillo-wire, imports as sillo_wire.
from sillo import SilloApp
from sillo_wire import Hub, Peer
app = SilloApp()
hub = Hub()
@app.ws_route("/ws/room/{name}")
async def room(socket, name: str):
await socket.accept()
peer = Peer(socket, identity=socket.query_params.get("user"))
await hub.join(peer, name)
try:
async for message in socket.iter_json():
await hub.broadcast(name, message)
finally:
await hub.disconnect(peer)
Why this exists
Three things differ from the obvious implementation, and they are the whole point of the package.
A broadcast never blocks. Writing straight to each socket in turn means the slowest member of a room sets the pace for everyone else — a client that has stopped reading fills its kernel buffer, the write blocks, and the rest of the room waits behind it. Here every peer has a bounded queue and a writer task, so a broadcast only ever enqueues:
report = await hub.broadcast("lobby", {"msg": "hello"})
report.delivered # 41
report.dropped # 2 queues were full
report.failed # 1 socket was already gone
You get a DeliveryReport rather than nothing, because a fan-out you cannot
measure is a fan-out you cannot operate.
Nothing is global. A Hub is an ordinary object. Two of them are two
independent worlds, so tests get a fresh one per case instead of remembering to
flush shared state, and a multi-tenant application keeps traffic apart without
a naming convention.
History is replayable. Every envelope carries a monotonic sequence, so a client that reconnects asks for what it missed rather than for everything or for nothing:
await hub.replay(peer, "lobby", since=last_seq_the_client_saw)
Slow consumers
When a peer's queue fills, what happens is a choice, not a default:
from sillo_wire import Overflow, Peer
Peer(socket, overflow=Overflow.DROP_OLDEST) # keep current — prices, cursors
Peer(socket, overflow=Overflow.DROP_NEWEST) # keep order — reconcile later
Peer(socket, overflow=Overflow.CLOSE) # disconnect and let it reconnect
Presence
@hub.on_join
async def joined(room, peer):
await hub.broadcast(room, {"event": "joined", "who": peer.identity})
hub.identities("lobby") # ["ada", "bob"] — people, not sockets
hub.count("lobby") # 5 — subscriptions
Two peers can share an identity — the same person with a phone and two tabs —
and send_to reaches all of them:
await hub.send_to("ada", {"notice": "your export is ready"})
Consumers
RoomConsumer is the class-based form. It accepts the socket, builds the peer,
joins the rooms, pumps messages, and guarantees the peer is removed from every
room when the connection ends — including when a hook raises.
from sillo_wire import Hub, RoomConsumer
hub = Hub()
class Chat(RoomConsumer):
hub = hub
async def identify(self, ctx):
return ctx.query_params.get("user")
async def rooms(self, ctx):
return [ctx.path_params["room"]]
async def on_message(self, data):
await self.broadcast({"from": self.peer.identity, "text": data})
app.add_ws_route(path="/ws/{room}", handler=Chat.as_handler())
Backlog
Retention is per room and capped by payload bytes, evicting oldest first:
from sillo_wire import Hub, MemoryBacklog, NullBacklog
Hub(backlog=MemoryBacklog(capacity_bytes=4 * 1024 * 1024))
Hub(backlog=NullBacklog()) # keep nothing — typing indicators, telemetry
Backlog is a Protocol, so a Redis or Postgres store satisfies it without
importing anything from here.
Testing
sillo_wire.testing ships the piece unit tests are missing — a socket:
from sillo_wire import Hub, Peer
from sillo_wire.testing import FakeSocket, drain
async def test_a_broadcast_reaches_the_room():
hub, socket = Hub(), FakeSocket()
peer = Peer(socket)
await hub.join(peer, "lobby")
await hub.broadcast("lobby", {"hello": True})
await drain(peer) # broadcasts enqueue; this waits for the write
assert socket.sent == [{"hello": True}]
FakeSocket(delay=…) simulates a client that is slow to read, and
FakeSocket(fail=True) one that has gone away — the two cases that are hardest
to reproduce against a real server and the two most worth testing.
Reference
Hub |
join leave leave_all disconnect broadcast send_to replay history clear_history on_join on_leave rooms members identities count prune close |
Peer |
offer send start close is_idle closed pending identity |
Envelope |
payload room seq sent_at size() |
DeliveryReport |
delivered dropped failed attempted |
Backlog |
MemoryBacklog NullBacklog, or your own |
Overflow |
DROP_OLDEST DROP_NEWEST CLOSE |
Working on it
The alias works under an editable install too — the .pth is shipped by the
editable build target as well as the wheel.
pip install -e ".[dev]"
pytest --cov # 100% required, bootstrap included
ruff check sillo_wire tests
mypy sillo_wire
Requirements
Python 3.10+, sillo-framework 0.3 or newer. No other dependencies.
Licence
BSD-3-Clause.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sillo_wire-1.0.0a2.tar.gz.
File metadata
- Download URL: sillo_wire-1.0.0a2.tar.gz
- Upload date:
- Size: 87.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f74691d0ea43335b9778102f741b5c1fa33f1361db3d97e68dca570e191328fe
|
|
| MD5 |
06528de04929507fb0ff73963904a6c2
|
|
| BLAKE2b-256 |
c197170ebca2b3e41b5614f5af0c542c69e82eb5fa810d8821fca665c482ad99
|
File details
Details for the file sillo_wire-1.0.0a2-py3-none-any.whl.
File metadata
- Download URL: sillo_wire-1.0.0a2-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33d84ed224e719e6383780b4d54af4727b6c5986e755c4a02d9337c754b00777
|
|
| MD5 |
963784559b42396fdcfc7a431c00be21
|
|
| BLAKE2b-256 |
65936439a9138033be8adc956ef60f9165dd9ebd4c73b9465754feca61a677a3
|