Topics and replay
A topic in PopSub is a log. When you publish to orders/42, the server appends the message to
that topic’s log, numbers it, and only then sends it to subscribers. That number is the
message’s seq. The first message in a topic is 1, the next is 2, and the count never goes
back, even after old messages are deleted.
Since the messages are kept, subscribing comes down to one question: where in the log do you want to start reading?
The top row is a topic that has had ten messages. The first three are old enough to have been trimmed. Each row below is a different subscriber. The solid part is what the server replays from the log, and every subscriber then carries on with live messages from 11.
Picking a starting point
Section titled “Picking a starting point”You choose with from when you subscribe. Asking for {seq: 1} is the usual way to say
“everything you still have”. If the message you asked for is gone, as in the first row, you
start at the oldest one left and get a truncated notice first, so you know you missed
something.
{last: N} is for “show me recent activity”, and {since_ms: T} starts at the first message
published at or after a time, in milliseconds since the epoch. "new" skips history
altogether.
"resume" is the default. It continues from where your durable session got to, which is
what makes a worker pick up after a restart. Without a session there’s nothing to resume
from, so it behaves like "new". Durable sessions explains
how that works, and one case where {seq: 1} is the better choice.
Each client library spells these slightly differently:
| Wire | TypeScript | Python | Go | Rust |
|---|---|---|---|---|
"resume" |
"resume" |
StartFrom.resume() |
popsub.FromResume |
StartFrom::Resume |
"new" |
"new" |
StartFrom.new() |
popsub.FromNew |
StartFrom::New |
{"last": N} |
{ last: n } |
StartFrom.last(n) |
popsub.FromLast(n) |
StartFrom::Last(n) |
{"since_ms": T} |
{ sinceMs: t } |
StartFrom.since_ms(t) |
popsub.FromSinceMs(t) |
StartFrom::SinceMs(t) |
{"seq": S} |
{ seq: s } |
StartFrom.seq(s) |
popsub.FromSeq(s) |
StartFrom::Seq(s) |
So in TypeScript, the last ten orders and then everything new looks like this:
const orders = await client.subscribe("orders/42", { last: 10 });
for await (const message of orders) { console.log(`#${message.seq}: ${message.payload}`);}From history to live, with no gap
Section titled “From history to live, with no gap”Before replaying anything, the server tells you exactly which range it’s about to send:
{ "type": "subscribed", "topic": "orders/42", "replay_from": 33, "replay_to": 42 }You get 33 to 42 from the log, then 43 onwards as they’re published. Nothing slips between the two and nothing arrives twice. Each topic has a single writer that handles its publishes and subscribes one at a time, so a publish can’t land halfway through setting up your subscription.
Order only holds within a topic. Two messages on orders/42 always arrive in seq order, but
a message on orders/42 and one on orders/43 can arrive either way round.
How long messages are kept
Section titled “How long messages are kept”By default each topic keeps its last 1,000 messages, for up to an hour, and up to 64 MiB. Whichever limit is hit first trims the oldest messages. The server checks after every publish and once a minute, so a quiet topic still ages out.
Retention doesn’t wait for slow readers. If a subscriber falls so far behind that the messages
it was owed are gone, it gets a truncated frame saying where the log now starts, and carries
on from there:
{ "type": "truncated", "topic": "orders/42", "first_available": 1300 }Some topics deserve more history than others. You can set limits per topic in the config file:
[[storage.topic_retention]]pattern = "audit/#"max_age_secs = 2592000
[[storage.topic_retention]]pattern = "presence/+"max_messages = 1The first entry a topic matches wins, so put specific patterns first. Any limit an entry leaves
out comes from the defaults: audit/# above keeps 30 days, but still no more than 1,000
messages. These entries only work in the config file, and changing them needs a restart.
Naming topics
Section titled “Naming topics”A topic exists the moment something is published to it, so there’s nothing to create first.
Use / between levels, as in orders/42 or sensors/attic/temp, and keep names under 120
bytes. + and # aren’t allowed in a topic name because they’re wildcards; see
Wildcards and groups.
Topic names appear unmasked in logs and metrics, so keep personal data out of them.