QoS and acknowledgements
Every message is stored in its topic’s log and numbered, whatever its QoS. The QoS only decides two things: whether the publisher hears back, and whether the server waits for each subscriber to say it has the message.
At QoS 0 the server sends the message and moves on. It’s fine for things you can afford to miss, like a progress update that will be replaced a second later.
At QoS 1 the publisher is told once the message is stored, and each subscriber has to acknowledge it. Until it does, the server keeps resending:
Publishing
Section titled “Publishing”In the client libraries, publish is QoS 0 and publishConfirmed is QoS 1 (publish_confirmed
in Python and Rust, PublishConfirmed in Go):
client.publish("orders/42", "order 42 viewed");
const stored = await client.publishConfirmed("orders/42", "order 42 shipped");console.log(`stored as #${stored.seq}`);A confirmed publish only resolves once the message is stored. If the server can’t store it, the
publisher gets storage_unavailable and nobody receives anything.
Retrying a publish safely
Section titled “Retrying a publish safely”Say the connection drops after you sent a publish but before published came back. Was it
stored? You can’t tell, so you send it again. To make that safe, every message has a
message_id. If the same account publishes the same message_id to the same topic while the
original is still kept, the server doesn’t store it twice. A QoS 1 retry just gets the original
seq back.
The client libraries do this for you. Each confirmed publish gets a fresh UUID, and one that’s still waiting when the connection drops is resent with the same id after reconnecting. That covers retries inside one running client. If your app restarts and publishes the same thing again, that’s a new message.
Acknowledging
Section titled “Acknowledging”By default the client libraries acknowledge each message as they hand it to your code. That’s simple, but if your code crashes halfway through handling a message, it’s gone. For work that must not be lost, switch to manual mode and acknowledge when you’re done:
const client = await Client.connect({ url: "ws://127.0.0.1:8080", credentials: { username: "alice", password: "alice-password" }, sessionId: "billing", ackMode: "manual",});
const orders = await client.subscribe("orders/#", { seq: 1 });
for await (const message of orders) { await charge(message.payload); message.ack();}If the worker dies before ack(), the message comes back, either as a resend or, with a
durable session, when the worker starts again.
| Library | Manual mode | Acknowledge |
|---|---|---|
| TypeScript | ackMode: "manual" |
message.ack() |
| Python | ack_mode=AckMode.MANUAL |
message.ack() |
| Go | AckMode: popsub.AckManual |
message.Ack() |
| Rust | .ack_mode(AckMode::Manual) |
message.ack() |
When a subscriber falls behind
Section titled “When a subscriber falls behind”A subscriber can have at most 100 unacknowledged QoS 1 messages at a time (broker.max_inflight).
Once it reaches that, the server stops sending it anything on that topic, QoS 0 included, so the
order stays intact. New messages wait in the log and are sent as acknowledgements free up room.
A slow consumer is paced from disk instead of piling up in the server’s memory.
When a subscriber never acknowledges
Section titled “When a subscriber never acknowledges”As the diagram shows, an unacknowledged message is resent every 5 seconds
(broker.ack_timeout_ms), up to 5 times (broker.max_retries). After that the subscription
stalls: the client gets a stalled frame, and nothing more is sent on that topic until it
acknowledges something or subscribes again. Other subscribers aren’t affected.
A resend can arrive after newer messages, which is the one time you’ll see a topic out of order. The client libraries drop resends of messages they’ve already handed over.
If you’d rather move on than stall, name a dead-letter topic:
[broker]dead_letter_topic = "dead-letters"A message that runs out of retries is then copied there and the subscription carries on. The
copy’s payload is a JSON envelope with the original topic, seq, payload, publisher and
subscriber, and a reason. Subscribe to dead-letters to deal with failures. Note that the
envelope names both users, which normal messages never do, so grant access to it carefully.
What you can count on
Section titled “What you can count on”| Promise | |
|---|---|
| QoS 0 | At most once, to subscribers connected at the time |
| QoS 1 | At least once to each connected subscriber, and across reconnects for a durable session whose messages are still kept |
| Order | Per topic, in seq order, except for resends |
| Duplicates | Possible, and always detectable by topic and seq. The client libraries drop them. |
Exactly once isn’t offered. Make whatever your consumer does safe to repeat, and use seq to
spot repeats.