Set up notifications
Requirements
- PostgreSQL 16 for the notifications database.
- Read-only access to the clc-core database. The service reads
users,vouchersandpools. - NATS 2.10 or later with JetStream, with the
TRACKERstream from ETH Tracker. Start ETH Tracker on the same NATS server first. - Centrifugo v6 for in-app delivery.
- A VAPID key pair for Web Push.
- Go 1.26.3 to build from source, or Docker.
Try it locally
docker compose up starts PostgreSQL on port 5434, NATS, Centrifugo on port 8000, and the service on port 5005, all with development secrets.
git clone https://github.com/cosmo-local-credit/clc-app-notifications.git
cd clc-app-notifications
docker compose upSet NOTIFICATIONS_CORE__DSN in docker-compose.yml to your clc-core database first. The compose file points it at the notifications database, which has no users, so every lookup fails. deploy/centrifugo.json configures Centrifugo.
Configure
The service reads config.toml. Set -config to use another file. Values come from, highest first:
- Environment variables with the
NOTIFICATIONS_prefix, with two underscores between section and key.NOTIFICATIONS_POSTGRES__DSNsetspostgres.dsn. - A
.envfile in the same directory as the config file. config.toml.
Write each line break in a PEM value as \n. Every duration setting must be set, so keep config.toml and override only what changes.
Secrets
Set these as environment variables or in .env:
| Variable | Needed by | Value |
|---|---|---|
NOTIFICATIONS_POSTGRES__DSN | All roles | Notifications database |
NOTIFICATIONS_CORE__DSN | All roles | clc-core database, read only |
NOTIFICATIONS_NATS__URL | bridge | NATS server |
NOTIFICATIONS_API__PUBLIC_KEY | api | clc-core Ed25519 public key, PEM. Verifies the app's session cookie. |
NOTIFICATIONS_CURSOR__SECRET | api | 32 bytes or more. The same on every replica. |
NOTIFICATIONS_CENTRIFUGO__HMAC_SECRET | api | Centrifugo client.token.hmac_secret_key |
NOTIFICATIONS_CENTRIFUGO__API_KEY | worker | Centrifugo http_api.key |
NOTIFICATIONS_PUSH__PUBLIC_KEY, NOTIFICATIONS_PUSH__PRIVATE_KEY | api, worker | VAPID key pair, base64url |
Generate a cursor secret and a VAPID key pair:
openssl rand -base64 32
npx web-push generate-vapid-keysSettings
| Setting | What it controls |
|---|---|
service.roles | Roles this process runs: api, bridge, worker. |
core.chain_id | Chain of the clc-core vouchers and pools. 100 for Gnosis. The example uses 42220. |
nats.stream, nats.consumer | Tracker stream and durable consumer name. |
messaging.enabled | Reads the MESSAGING stream. Turn it on once clc-core publishes it. Needs the Centrifugo API key on the bridge. |
api.address | Listen address. The example uses :5005. |
api.prod | true in production. Requires a wss WebSocket URL and safe push endpoints. |
api.cors | App origins allowed to call the API. |
api.environment | Deployment name, built into inbox cursors. |
api.trust_proxy | Rate-limits by the first X-Forwarded-For address. Turn it on only behind a trusted proxy. |
centrifugo.api_url | Centrifugo HTTP API. |
centrifugo.websocket_url | Centrifugo WebSocket URL that the app connects to. |
push.subscriber | Contact URL sent to push services, such as mailto:. |
push.version | Name of the current VAPID key pair. |
policy.voucher_received.display_wait | Wait before Web Push when the user has the app open. |
policy.voucher_received.alert_lifetime | How long a voucher notification can still be pushed. |
policy.messaging.display_wait, policy.messaging.alert_lifetime | The same, for message, pool and Safe notifications. |
bridge.max_in_flight | Events the bridge handles at once. |
bridge.swap_wait_attempts | Times the bridge waits for a swap record before it notifies a transfer anyway. |
river.realtime_workers, river.push_workers | Workers for the in-app and push queues. |
devices.max_per_user | Devices a user can register. |
Grant access to clc-core
The clc-core database user needs SELECT on users, vouchers and pools:
GRANT SELECT ON users, vouchers, pools TO clc_notifications_core;Run
Every role runs the app and River migrations at startup, from migrations/ in the working directory.
From source, in the repository root:
make build
./build/clc-notifications -config config.tomlWith Docker:
docker run -d --name clc-app-notifications \
--env-file .env \
-v "$PWD/config.toml:/service/config.toml:ro" \
-p 5005:5005 \
ghcr.io/cosmo-local-credit/clc-app-notifications:<tag>To scale, run the roles as separate processes against the same database.
Verify
curl -s localhost:5005/healthz
curl -s localhost:5005/api/notifications/vapid-public-key
nats consumer info TRACKER clc-notifications/healthz only shows that the api role is up. It does not check the database, NATS or Centrifugo.
After a voucher transfer to an approved user, check the database:
SELECT kind, title, body, created_at FROM notifications ORDER BY created_at DESC LIMIT 5;
SELECT channel, status, accepted_at FROM notification_deliveries ORDER BY created_at DESC LIMIT 5;If the TRACKER stream does not exist when the bridge starts, the log shows bridge stopped and the bridge stops reading tracker events. Start ETH Tracker, then restart the service.