popsub (Python client)
An asyncio client for PopSub, a WebSocket publish/subscribe broker. It follows the behaviour every PopSub client shares (clients/README.md in the repository) and passes the shared scenario suite.
The client handles the protocol's bookkeeping so an application only connects, subscribes, publishes, receives and acknowledges:
- Logs in with a username and password (or takes a token) and authenticates with protocol version 1. With password credentials it logs in again when the server sends
auth_expiring. - Reconnects with jittered exponential backoff, and reads the close code to decide whether to: never after
auth_failed(4001),session_taken_over(4003),unsupported_protocol(4006) oraccount_disabled(4009), and aftertoken_expired(4004) only with password credentials. A rejected login on the first connection failsconnectwithout retrying. - Treats the connection as dead when nothing (pings included) arrives for
idle_timeout_secs + heartbeat_secsfrom the server'slimits. - After reconnecting, subscribes again from
resumeto topics the server resumed for a durable session when it holds them (held: true) and leaves them to the server otherwise, subscribes every other topic again from{"seq": next_expected}, resends unconfirmedpublish_confirmedcalls with the same request id andmessage_id, and sends QoS 0 publishes held while disconnected (up to 10,000; the oldest are dropped first). - Sends
hold_resumed: trueinauthwhenever it has asession_id, so that in a new process on an existing durable session the server sends nothing on a resumed topic until the application subscribes to it. When the server answersheld: true, that subscribe is an ordinary one and a topic the application never subscribes to has nothing in flight. - When the server answers without
held, neither hands over nor acknowledges messages on a resumed topic until the application subscribes to it by name or through a matching pattern, so the server keeps them. The subscribe then replays them: messages that arrive before itssubscribedare dropped unacknowledged, and a pattern sends asubscribeby name fromresumefor each resumed topic it matches. A resumed topic the application never subscribes to stays unacknowledged on the server. - Hands each
(topic, seq)to the application once. A duplicate is acknowledged again when it was acknowledged before, or always in automatic mode. - Stops reading the socket while a subscription's buffer is full, so the server paces delivery instead of the client buffering without bound.
- Ends a subscription and reports
Unsubscribedwhen the server ends it (anunsubscribedframe with no id). Every unsubscribe the client sends carries an id for that reason. - Treats a wildcard subscription (
orders/#) as one subscription: each topic the server joins for it is tracked on its own (next expectedseq, acknowledgements), and after a reconnect the tracked topics are resubscribed first and the pattern is sent again from{"seq": 1}, which joins only topics created meanwhile. Subscribing again to a held pattern replaces the subscription: its topics move to the new one with their next expectedseq, and the earlier one ends. - Deduplicates a shared subscription (a topic read through a
group) with a bounded set of the 4,096 most recently deliveredseqs, since a member gets only some messages and not in order, and rejoins the group withresumeafter a reconnect. - Never queues or resends admin requests: a disconnect fails them with
ConnectError, because the server may or may not have carried them out.
Delivery is what the server provides: QoS 1 is at-least-once per subscriber while connected, and across reconnects only for durable sessions whose messages are still retained. The client removes the duplicates that this produces.
Install
Python 3.10 or newer. The only runtime dependency is websockets.
pip install popsub
or uv add popsub. To use a version that isn't released yet, install it from a checkout: uv add path/to/popsub/clients/python.
Usage
import asyncio
from popsub import AckMode, Client, ClientConfig, PasswordCredentials, StartFrom
async def main() -> None:
config = ClientConfig(
"ws://127.0.0.1:8080",
PasswordCredentials("alice", "alice-password"),
session_id="alice-laptop",
ack_mode=AckMode.MANUAL,
)
async with await Client.connect(config) as client:
orders = await client.subscribe("orders", StartFrom.resume())
stored = await client.publish_confirmed("orders", "order 42 shipped")
print("stored as seq", stored.seq)
async for message in orders:
print(message.topic, message.seq, message.payload)
message.ack()
asyncio.run(main())
Events arrive on a separate stream:
async def watch(client: Client) -> None:
async for event in client.events():
print(event.to_json())
Binary payloads
A payload is a string. For a body that isn't text, publish bytes and read them back with message.data:
async def send_and_read(client: Client) -> None:
blobs = await client.subscribe("blobs", StartFrom.new())
await client.publish_bytes_confirmed("blobs", b"\x00\xff\xfe\x80")
message = await blobs.next()
if message is not None:
print(message.encoding, message.data)
publish_bytes (QoS 0) and publish_bytes_confirmed (QoS 1) send the bytes as standard padded base64 in payload with encoding: "base64", and otherwise behave like publish and publish_confirmed: held while disconnected, and resent with the same request id and message_id. Base64 adds about a third, and it is the encoded string that counts against the server's payload limit (payload_too_large).
On a received message, payload is the string as it arrived, encoding is None for text or Encoding.BASE64, and data is the bytes: the decoded payload for base64, otherwise the payload's UTF-8. The server checks base64 on publish and never decodes it. If a payload claims base64 and doesn't decode, the message is still delivered and data raises ProtocolError.
Wildcard subscriptions
A pattern uses + for any one level and a trailing # for the rest. One subscription yields the messages of every matching topic the user may read, including topics created later, each under its own topic and seq:
async def follow_orders(client: Client) -> None:
async with await client.subscribe("orders/#", StartFrom.new()) as orders:
async for message in orders:
print(message.topic, message.seq, message.payload)
client.unsubscribe("orders/#"), or closing the subscription, leaves the pattern and every topic joined through it. If the server ends one of those topics (access revoked), only that topic stops and Unsubscribed names it.
Subscribing again to the same pattern returns a new subscription that takes over the topics already joined, each from the next seq it expects, so every message still arrives once. The earlier subscription ends; closing it afterwards changes nothing.
Shared subscriptions
Members of a group split a topic's messages: each message goes to one member. A member sees its share, not in strict seq order, and a message can reach a second member after the first disconnects without acknowledging it, so workers must tolerate a repeat.
async def work(client: Client) -> None:
jobs = await client.subscribe_shared("jobs", "workers", StartFrom.new())
async for message in jobs:
print("handling", message.seq, message.payload)
message.ack()
start_from only decides where a group starts the first time it is used on a topic; after that the group's position is on the server.
API
Everything below is importable from popsub.
| Name | What it is |
|---|---|
await Client.connect(config) |
Connects and waits for the first authenticated connection, retrying with the reconnect policy for up to request_timeout. |
await client.subscribe(topic, start_from=StartFrom.resume()) |
Returns a Subscription. topic may be a wildcard pattern. StartFrom.resume(), .new(), .last(n), .since_ms(t) or .seq(s). |
await client.subscribe_shared(topic, group, start_from=StartFrom.resume()) |
Joins a shared subscription on an exact topic and returns a Subscription with this member's share. |
await client.unsubscribe(topic) |
Unsubscribes from a topic or pattern; its Subscription ends. |
await client.publish(topic, payload) |
QoS 0. Returns once queued; server failures arrive as ServerErrorEvent. |
await client.publish_confirmed(topic, payload) |
QoS 1 with a new UUID message_id. Returns Published(seq, message_id) once stored. |
await client.publish_bytes(topic, data) |
publish for bytes: sent as base64 with encoding: "base64". |
await client.publish_bytes_confirmed(topic, data) |
publish_confirmed for bytes. Returns Published(seq, message_id) once stored. |
client.admin() |
Admin with create_user, get_user, list_users, set_role, grant, revoke, set_password, set_enabled, delete_user. They return UserInfo (a list for list_users, None for delete_user). The topic calls list_topics, get_topic(topic) and purge_topic(topic) return TopicInfo (name, first_seq, last_seq, messages, subscribers; a list for list_topics); a topic nothing was published to gives ServerError with code topic_not_found. |
client.events() |
An EventStream: an async iterator of events from now on, ending after the client stops. It keeps the latest 256 events for a slow reader. |
await client.close() |
Closes the connection and stops the client. Every task it started has finished when this returns. |
client.stopped, client.close_code |
Whether the client stopped for good, and the close code that stopped it. |
Subscription |
async for message in subscription, or await subscription.next() (None at the end). subscription.close() stops listening and unsubscribes without waiting. replay_from and replay_to come from the server's subscribed. |
Message |
topic, seq, message_id, payload, qos, published_at_ms, encoding (None for text, or Encoding.BASE64), data (the message's bytes; raises ProtocolError when a base64 payload doesn't decode), and ack(), which does something only for QoS 1 in manual mode and is safe to call twice. |
Events: Connected(resumed), Disconnected(code, reconnecting), Truncated(topic, first_available), Stalled(topic, seq), AuthExpiring(expires_at_ms), AuthRefreshed(), Unsubscribed(topic) and ServerErrorEvent(code, message). Each has to_json().
Configuration
ClientConfig(url, credentials, ...):
| Field | Default | Meaning |
|---|---|---|
url |
ws:// or wss:// |
|
credentials |
PasswordCredentials(username, password) or TokenCredentials(token) |
|
session_id |
None |
A durable session to hold; None is ephemeral. |
clean_start |
False |
Discard the session's stored subscriptions; sent on the first connection only. |
ack_mode |
AckMode.AUTO |
AUTO acknowledges QoS 1 messages as they are handed over; MANUAL waits for message.ack(). |
reconnect |
ReconnectPolicy(0.25, 30.0, None) |
initial and max ceilings in seconds, and max_attempts (None keeps trying). |
request_timeout |
30.0 |
Seconds a connect, subscribe, confirmed publish or admin call may take. |
subscription_buffer |
1024 |
Messages buffered per subscription before the client stops reading the socket. |
ssl_context |
None |
TLS settings for wss://, such as a context trusting a private CA. |
Errors
Every failure is a PopSubError with a kind:
| Class | kind |
Extra attributes |
|---|---|---|
ServerError |
server |
code (such as not_authorized), retry_after |
ClosedError |
closed |
close_code (or None) |
RequestTimeoutError |
timeout |
|
ConnectError |
connect |
|
ProtocolError |
protocol |
|
ConfigError |
config |
error.is_temporary says whether trying again later might work. Errors keep their cause in __cause__.
Logging
The client logs through the standard logging module under the popsub logger and configures no handlers. Its own log lines never include passwords, tokens or payloads. The websockets library logs under its own websockets logger, and at DEBUG level that includes frame contents, so keep it above DEBUG in production.
Scenario suite
The scenario driver is the internal module popsub._driver; it is not part of the public API. From the repository root, with the suite built (cargo build -p popsub_scenarios):
target/debug/popsub-scenarios --driver "uv run --project clients/python python -m popsub._driver"
target/debug/popsub-scenarios --driver "uv run --project clients/python python -m popsub._driver" --scenario reconnect_resubscribes_without_gaps
The driver logs to standard error; set POPSUB_LOG=DEBUG for more.
Development
Uses uv. From clients/python:
uv sync
uv run ruff format --check . && uv run ruff check . && uv run mypy && uv run pytest
That runs the formatter check, the linter, mypy --strict over the package and tests, and the tests. The tests cover backoff, deduplication (by next expected seq, and by the seen set for shared subscriptions), the wildcard pattern matcher, the wire types and a message's bytes, feed the engine frames directly to check how a new process claims a durable session's topics and how a repeated pattern subscribe moves its topics, and run the client against a small in-process fake server to check that resent copies are dropped and acknowledged again, that a reconnect resubscribes from the next expected seq, that byte publishes carry base64 and the encoding (also when resent) while text publishes carry none, and that close() leaves no task running.
uv build writes the wheel and source distribution to dist/.
Metadata
Release files for popsub 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| popsub-0.1.0.tar.gz | 46.9 kB | With provenance | Details for popsub-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| popsub-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for popsub-0.1.0-py3-none-any.whl |
Total release size: 100.0 kB
Release files / popsub-0.1.0.tar.gz
| Download URL | popsub-0.1.0.tar.gz |
|---|---|
| Size | 46.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
06fa63c55e7688dcc70481264e11b7868a66b15c0449fee0d8d0aa955c0c15bf
|
|
BLAKE2b-256 checksum How to use checksums |
314c3b7719f4a01aed2089cd2288583ecb4a5b19ea794894c2002b2aae22b090
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / popsub-0.1.0-py3-none-any.whl
| Download URL | popsub-0.1.0-py3-none-any.whl |
|---|---|
| Size | 53.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
751328f62f5ec85d7c55a47d2b244bc69a1e116178177eb76ab1b4082a954e55
|
|
BLAKE2b-256 checksum How to use checksums |
68eb77ed199d35bc8fda71d1b14fe6e3dfe5339628f2948150bc1ba23d55d72a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for popsub 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for popsub-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.