Breaking: with state.dir set, jobs still waiting when the daemon stops or crashes print after the next start, in order and however old, instead of being marked failed or forgotten; a client that resends such jobs must stop. The Docker image sets state.dir, so a container keeps its jobs in /var/lib/uberbringer, and loses them with the container unless a volume is mounted there. The owner decided that jobs survive a restart and that a restart never prints a receipt twice. When state.dir is set, the daemon now opens the SQLite job store there, jobs.db, and the queue restores it before the subscriptions are made and before anything is served, so restored jobs go ahead of new ones and a second daemon on the directory stops before it reads the subscriptions' state. Without state.dir the queue keeps its jobs in memory, as before. The store closes once the queue has stopped. A job's sending mark is synced before its first byte, so one caught mid write reads uncertain after the restart and is never sent again; a job that had not reached the printer prints once. A store that cannot be opened stops the start with an error naming the file, rather than starting empty and forgetting which receipts are in doubt. The unit gains UMask=0077, so the store's files are its user's alone, and says why systemd's 90 s stop timeout is enough; the Dockerfile and docs/docker.md ask for --stop-timeout 40. design.md replaces "The queue lives in memory" with the store, the state machine, the sync policy, store failures, stage 1a's numbers, and that the ARM builds ship the store untested on ARM hardware. install.md and docker.md say how to set aside a store that will not open; configuration.md, api.md, cli.md and ntfy.md say what survives a restart. The license notices gain the driver's modules. TestServeRestart runs the daemon twice on one directory: jobs queued when the first run stops, its printer refusing connections, print in order after the restart, ahead of a new job, and a job seeded in sending between the runs reads uncertain and never reaches the printer. Part of stage 1d of docs/plans/job-store.md (decisions 14 and 15). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8.8 KiB
ntfy subscriptions
The daemon can subscribe to ntfy topics itself and print each message as a notification card, so the phone's ntfy app, or anything else that publishes, is the only client. To have the ntfy command line client post messages instead, see Printing from the ntfy client.
Contents
Setting one up
Each [[ntfy.subscriptions]] table in config.toml is one connection to
one server, for one or more topics. Subscriptions are set in the file only,
not by environment variables. configuration.md
lists every key, and ntfy the settings that shape
the cards.
[state]
dir = "/var/lib/uberbringer"
[[ntfy.subscriptions]]
name = "home"
server = "https://ntfy.example.com"
topics = ["alerts", "backups"]
token_file = "/etc/uberbringer/ntfy.token"
[ntfy.subscriptions.quiet]
start = "22:00"
end = "07:00"
install.sh and the Docker image set state.dir already. In Docker, mount
a volume for the state and the token file next to the config, as
docker.md shows.
/healthz lists each subscription's state, counts and last error; see
Health.
How a subscription behaves
- First start. With no state yet, it prints only messages published from then on, also after a reconnect soon after.
- Restarts and outages. It keeps a cursor, the newest server time it
has dealt with, and when it reconnects or starts again it asks for the
messages since then, two minutes earlier to catch late ones. Of those,
it prints only the ones no older than
max_backlog, measured by ntfy's own clock as the stream tells it (the time of its first,openevent, or else the answer'sDate), never by this host's, and leaves the older ones out as they arrive; they stay out, even after a restart with a longer backlog. It keeps the IDs of the messages a replay could send again, at least the last 512 and at most 20,000, so a message the server sends again, after a reconnect or as ntfy's own repeat, never prints twice, even after a flood; past 20,000, the next replay starts after the IDs it forgot. - Failures. A dropped connection is opened again after 5, 10, 20, 30,
60 and then every 120 seconds, each ± 20%, from 5 seconds again once a
connection has lasted two minutes. A
429waits as long as itsRetry-Aftersays. Refused credentials (401or403) are checked at startup, and while the server refuses them the subscription isunauthorizedin/healthzand tries every five minutes. Printing goes on meanwhile. - A full print queue pauses the subscription: it stops reading the stream, offers the message again after 1 second, then after twice as long each time, up to 30 seconds, and moves its cursor only once the message is queued. A digest that cannot be queued waits the same way.
- The paper budget. At most
rate_limitreceipts print in anyrate_window, digests included. Messages over it are held and print together on one digest once the budget has room. Outside quiet hours, messages ofbypass_priorityand above print at once. - Quiet hours. During the window, only
quiet.min_priorityand above print. The rest are held for anOvernightdigest when the window ends, or left out withaction = "drop". - Updates. ntfy lets a publisher replace a notification with a later
message of the same sequence (
sequence_id). When a message's sequence printed before, on a card or a digest row (not only counted on a digest), and is among the last 512 it remembers, its card says(updated), or withntfy.format.updates = "skip"it is left out. The first message of a sequence prints plain, even when its publisher named the sequence. - State. The cursor, the IDs, the budget and the held messages are in
<state.dir>/ntfy-<name>.json, written through a temporary file and a rename, so a crash leaves the old file or the new one. It is written within a second of a message being printed, held or left out, at most every 10 minutes when only keepalives have moved the cursor, and at shutdown. A damaged file is ignored with a warning, and the subscription starts as if new. Held messages survive a restart, and so does a digest made and not yet queued: it is saved before it is queued, and a restart queues it again, unchanged. While the file cannot be written, digests wait. - Jobs. Each card and digest is an ordinary job with the source
ntfy:<name>and a reference: a card's is its message's ID, and a digest's is its own,digest_and a TypeID. A card or digest whose source and reference the queue already holds is not queued again. Each is in the job store, instate.dir, before it is queued: one still waiting when the daemon stops or crashes prints after the restart, and a message the restart replays prints once.
Securing the topics
Warning
Whoever can publish to a subscribed topic prints on your printer, and with priority 5 past the paper budget. On the public
ntfy.sh, anyone who knows a topic's name can publish to it: reserve the topic so that others may not write to it, or at the very least use a long random name. Your own ntfy server lets everyone publish too, unless access control is on: setauth-file, setauth-default-accesstodeny-all(orread-only), since it defaults toread-write, and give write access only to your publishers.
On your own ntfy server, with in its server.yml
auth-file: /var/lib/ntfy/user.db
auth-default-access: deny-all
give the printer a user of its own that may only read its topics, and each publisher one that may only write to them:
sudo ntfy user add printer # asks for a password
sudo ntfy access printer alerts read-only # once for each topic
sudo ntfy user add phone
sudo ntfy access phone alerts write-only
The printer's access token
Create the printer's access token and write it straight into a file only
the daemon can read, so that it never shows on the screen, on a command
line or in your shell history. The scripts below take both of
ntfy token add's output streams, since ntfy 2.13 and earlier print the
token on standard error, and hand the token to install on its standard
input. umask 077 keeps the file private from the moment it is created,
also with BusyBox's install, which sets the mode only after writing.
With ntfy on the same host:
out=$(sudo ntfy token add printer 2>&1)
token=$(printf '%s\n' "$out" | grep -o 'tk_[A-Za-z0-9]*')
if [ -z "$token" ]; then
printf 'ntfy made no token:\n%s\n' "$out" | sed 's/tk_[A-Za-z0-9]*/tk_.../g' >&2
elif printf '%s\n' "$token" | (umask 077; sudo install -m 600 -o uberbringer -g uberbringer /dev/stdin /etc/uberbringer/ntfy.token); then
unset token
else
cat >&2 <<'EOF'
The token was made but not stored. Fix the cause, then run:
printf '%s\n' "$token" | (umask 077; sudo install -m 600 -o uberbringer -g uberbringer /dev/stdin /etc/uberbringer/ntfy.token) && unset token
EOF
fi
unset out
With ntfy on another host, write the token there into a file of your own,
ntfy.token in the current directory:
out=$(sudo ntfy token add printer 2>&1)
token=$(printf '%s\n' "$out" | grep -o 'tk_[A-Za-z0-9]*')
if [ -z "$token" ]; then
printf 'ntfy made no token:\n%s\n' "$out" | sed 's/tk_[A-Za-z0-9]*/tk_.../g' >&2
elif printf '%s\n' "$token" | (umask 077; install -m 600 /dev/stdin ntfy.token); then
unset token
else
cat >&2 <<'EOF'
The token was made but not stored. Fix the cause, then run:
printf '%s\n' "$token" | (umask 077; install -m 600 /dev/stdin ntfy.token) && unset token
EOF
fi
unset out
Copy it to the printer's host, place it there with
sudo install -m 600 -o uberbringer -g uberbringer ntfy.token /etc/uberbringer/ntfy.token,
and delete both copies of ntfy.token.
When no token comes out, the script shows what ntfy said, with any token
in it masked, such as that the user does not exist. When a token was made
but could not be stored, for example because /etc/uberbringer is
missing, nothing is written, and the token stays in $token until the
printed line stores it. Never look it up with ntfy token list, which
prints every token in full; removing one with ntfy token remove needs
the token on its command line.
If the ntfy web app lets users sign in (enable-login: true, which is off
by default), you can also create the token there under Account and paste
it in with sudoedit, after
sudo install -m 600 -o uberbringer -g uberbringer /dev/null /etc/uberbringer/ntfy.token
has made the empty file.