Protocol
You only need this page to write a client in a new language. All you need is a WebSocket
library and a JSON parser. Everything here is defined in the
popsub_protocol
crate, and docs/protocol/ has a JSON Schema for every frame.
Frames
Section titled “Frames”Each frame is one JSON object in a WebSocket text message, with a type saying what it is:
{ "type": "publish", "id": 4, "topic": "orders/42", "payload": "order 42 shipped", "qos": 1 }Put an id on any request, a number or a string, and the reply carries it back. Match replies
by id, not by the order they arrive in: in the session below, a message overtakes the
published reply to the same publish.
Frames the server sends on its own, like message, truncated and stalled, have no id.
Payloads are always strings. To send bytes, base64 them and add "encoding": "base64". The
server stores the string as it is and never decodes it.
A connection’s life
Section titled “A connection’s life”A connection logs in, then sends auth with the token. Anything other than login, register
or ping before that is refused and the connection is closed. Once authenticated, it can do
everything else. A minute before the token expires the server sends auth_expiring, and the
client logs in again and sends a fresh auth on the same connection.
The server sends a WebSocket ping every 30 seconds and closes a connection it hasn’t heard from
in 90. WebSocket libraries answer pings on their own. In a browser, where you can’t see them,
send {"type":"ping"} now and then instead.
auth also carries the protocol version, currently 1. Within a version, frames, fields and
error codes are only ever added. Ignore fields and frame types you don’t know, and treat an
unknown error code like internal.
A real session
Section titled “A real session”Recorded against a local server with a short Node script. → is the client, ← the server,
and only the token is shortened.
→ {"type":"login","id":1,"username":"alice","password":"alice-password"}← {"id":1,"type":"logged_in","token":"eyJ..."}→ {"type":"auth","id":2,"token":"eyJ...","protocol":1,"session_id":"orders-worker","hold_resumed":true}← {"id":2,"type":"authenticated","protocol":1,"session_id":"orders-worker","held":true,"limits":{"max_inflight":100,"session_expiry_secs":86400,"heartbeat_secs":30,"idle_timeout_secs":90,"max_payload_bytes":1048576,"max_topic_bytes":120}}→ {"type":"subscribe","id":3,"topic":"orders/42","from":{"seq":1}}← {"id":3,"type":"subscribed","topic":"orders/42","replay_from":1,"replay_to":0}→ {"type":"publish","id":4,"topic":"orders/42","payload":"order 42 shipped","qos":1}← {"type":"message","topic":"orders/42","seq":1,"message_id":"fcdeb6b4-5768-4f66-972b-fefa068a9154","payload":"order 42 shipped","qos":1,"published_at_ms":1795413947884}← {"id":4,"type":"published","message_id":"fcdeb6b4-5768-4f66-972b-fefa068a9154","seq":1}→ {"type":"ack","topic":"orders/42","seq":1}→ {"type":"publish","id":6,"topic":"news/today","payload":"hello","qos":1}← {"id":6,"type":"error","code":"not_authorized","message":"not authorized to publish to 'news/today'"}replay_from 1 and replay_to 0 means there was no history to replay. The ack has no reply.
The error carries the id of the request it answers, and the connection stays open.
Connecting again later with the same session_id, the server lists the session’s topics and
resumes after the acknowledged message:
← {"id":2,"type":"authenticated","protocol":1,"session_id":"orders-worker","resumed":["orders/42"],"held":true,"limits":{...}}→ {"type":"subscribe","id":3,"topic":"orders/42","from":"resume"}← {"id":3,"type":"subscribed","topic":"orders/42","replay_from":2,"replay_to":1}Requests
Section titled “Requests”| Request | Fields | Reply |
|---|---|---|
login |
username, password |
logged_in with a token |
register |
username, password |
registered, if the server allows it |
auth |
token, protocol, session_id, clean_start, hold_resumed |
authenticated |
subscribe |
topic, from, group |
subscribed, then history, then live messages |
unsubscribe |
topic |
unsubscribed |
publish |
topic, payload, qos, message_id, encoding |
published at QoS 1, nothing at QoS 0 |
ack |
topic, seq |
nothing |
ping |
pong |
Only topic, payload, token, username, password and seq are required. from takes
"resume", "new", {"last": N}, {"since_ms": T} or {"seq": S}, as described in
Topics and replay. hold_resumed is explained in
Durable sessions.
What the server sends
Section titled “What the server sends”| Frame | Fields | When |
|---|---|---|
message |
topic, seq, message_id, payload, qos, published_at_ms, encoding |
For every message delivered, replayed or live |
subscribed |
topic, replay_from, replay_to, group |
After subscribe, and for each topic a pattern joins |
truncated |
topic, first_available |
Messages you were due have been trimmed |
stalled |
topic, seq |
A QoS 1 message ran out of retries |
unsubscribed |
topic |
After unsubscribe, or when access was revoked |
auth_expiring |
expires_at_ms |
A minute before the token expires |
error |
code, message, retry_after |
A request failed. See Errors and close codes. |
authenticated carries a limits object with the numbers a client needs: max_inflight,
session_expiry_secs, heartbeat_secs, idle_timeout_secs, max_payload_bytes and
max_topic_bytes.
Admin requests
Section titled “Admin requests”An enabled admin can manage accounts and topics over the same connection. Anyone else gets
forbidden. These are what popsub users and popsub topics send.
| Request | Fields |
|---|---|
create_user |
username, password, role |
get_user, delete_user |
username |
list_users, list_topics |
|
set_role |
username, role |
grant |
username, pattern, access |
revoke |
username, pattern |
set_password |
username, password |
set_enabled |
username, enabled |
get_topic, purge_topic |
topic |
A role looks like {"kind": "user", "topics": [{"pattern": "orders/#", "access": "both"}]},
where kind is admin, user or readonly and access is subscribe, publish or both.
JSON Schemas
Section titled “JSON Schemas”docs/protocol/ is generated
from the protocol types, and CI fails if it falls out of date. client.schema.json covers every
frame a client sends, server.schema.json every frame the server sends, and close-codes.json
the close codes and whether to reconnect after each. Generate your client’s types from them.
A library needs more than the frames to be reliable: reconnecting, resubscribing without
gaps, dropping duplicates. clients/README.md
lists all of it, and a shared scenario suite holds every library in the repository to it.