Skip to content

Set up notifications ​

Requirements ​

  • PostgreSQL 16 for the notifications database.
  • Read-only access to the clc-core database. The service reads users, vouchers and pools.
  • NATS 2.10 or later with JetStream, with the TRACKER stream 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.

sh
git clone https://github.com/cosmo-local-credit/clc-app-notifications.git
cd clc-app-notifications
docker compose up

Set 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:

  1. Environment variables with the NOTIFICATIONS_ prefix, with two underscores between section and key. NOTIFICATIONS_POSTGRES__DSN sets postgres.dsn.
  2. A .env file in the same directory as the config file.
  3. 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:

VariableNeeded byValue
NOTIFICATIONS_POSTGRES__DSNAll rolesNotifications database
NOTIFICATIONS_CORE__DSNAll rolesclc-core database, read only
NOTIFICATIONS_NATS__URLbridgeNATS server
NOTIFICATIONS_API__PUBLIC_KEYapiclc-core Ed25519 public key, PEM. Verifies the app's session cookie.
NOTIFICATIONS_CURSOR__SECRETapi32 bytes or more. The same on every replica.
NOTIFICATIONS_CENTRIFUGO__HMAC_SECRETapiCentrifugo client.token.hmac_secret_key
NOTIFICATIONS_CENTRIFUGO__API_KEYworkerCentrifugo http_api.key
NOTIFICATIONS_PUSH__PUBLIC_KEY, NOTIFICATIONS_PUSH__PRIVATE_KEYapi, workerVAPID key pair, base64url

Generate a cursor secret and a VAPID key pair:

sh
openssl rand -base64 32
npx web-push generate-vapid-keys

Settings ​

SettingWhat it controls
service.rolesRoles this process runs: api, bridge, worker.
core.chain_idChain of the clc-core vouchers and pools. 100 for Gnosis. The example uses 42220.
nats.stream, nats.consumerTracker stream and durable consumer name.
messaging.enabledReads the MESSAGING stream. Turn it on once clc-core publishes it. Needs the Centrifugo API key on the bridge.
api.addressListen address. The example uses :5005.
api.prodtrue in production. Requires a wss WebSocket URL and safe push endpoints.
api.corsApp origins allowed to call the API.
api.environmentDeployment name, built into inbox cursors.
api.trust_proxyRate-limits by the first X-Forwarded-For address. Turn it on only behind a trusted proxy.
centrifugo.api_urlCentrifugo HTTP API.
centrifugo.websocket_urlCentrifugo WebSocket URL that the app connects to.
push.subscriberContact URL sent to push services, such as mailto:.
push.versionName of the current VAPID key pair.
policy.voucher_received.display_waitWait before Web Push when the user has the app open.
policy.voucher_received.alert_lifetimeHow long a voucher notification can still be pushed.
policy.messaging.display_wait, policy.messaging.alert_lifetimeThe same, for message, pool and Safe notifications.
bridge.max_in_flightEvents the bridge handles at once.
bridge.swap_wait_attemptsTimes the bridge waits for a swap record before it notifies a transfer anyway.
river.realtime_workers, river.push_workersWorkers for the in-app and push queues.
devices.max_per_userDevices a user can register.

Grant access to clc-core ​

The clc-core database user needs SELECT on users, vouchers and pools:

sql
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:

sh
make build
./build/clc-notifications -config config.toml

With Docker:

sh
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 ​

sh
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:

sql
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.