Every app on the homelab's message queue publishes under its own name. Launcher's events go to the NATS JetStream stream EVENTS_LAUNCHPAD on the subject events.launchpad.<category>.<type>, as JSON. Business events are changes to people's dashboards, technical events are everything else, so events.launchpad.business.> takes Launcher's dashboard changes, and events.*.business.> every app's.
Events carry ids, never content: read the details through the API. The stream keeps them for 7 days. Delivery is at least once and not always in order, so make handling an event twice harmless (its id is unique) and read current state.
Connecting takes a login on the message queue from the person who runs this server. Every app shares one account there, which is how apps integrate. An API key doesn't open it, and anything connected sees every account's events.
events.launchpad.business.dashboard.changedSomeone's dashboard changed: a save, a sync, or a profile, tile or tag edit · userId, dashboardId, revision, change, clientId, adminId
events.launchpad.technical.dashboard.member_addedAn administrator let an application act on someone's dashboard · userId, adminId, subject, dashboardId
events.launchpad.business.clipboard.item_addedSomeone kept a text or a file on their clipboard (never its content) · userId, itemId, kind
events.launchpad.business.clipboard.item_removedSomeone removed an item from their clipboard · userId, itemId, kind
events.launchpad.business.clipboard.item_sharedSomeone made a clipboard file public to everyone on the server · userId, itemId, kind
events.launchpad.business.clipboard.item_unsharedSomeone made a public clipboard file private · userId, itemId, kind
events.launchpad.technical.dashboard.member_removedAn administrator stopped an application acting on someone's dashboard · userId, adminId, subject, dashboardId
events.launchpad.technical.user.signed_inSomeone signed in to the web or admin app · userId, client
events.launchpad.technical.user.signed_outSomeone signed out of the web or admin app · userId, client
events.launchpad.technical.appearance.updatedSomeone changed the appearance · userId, client
events.launchpad.technical.servers.updatedSomeone changed their server list · userId
events.launchpad.technical.avatar.updatedSomeone set a profile photo · userId
events.launchpad.technical.avatar.removedSomeone removed their profile photo · userId
events.launchpad.technical.api_key.createdAn API key was created · userId, apiKeyId
events.launchpad.technical.api_key.deletedAn API key was revoked · userId, apiKeyId
events.launchpad.technical.device.renamedA device was renamed · userId, deviceId
events.launchpad.technical.device.signed_outA device was signed out from another one · userId, deviceId
events.launchpad.technical.ai.command_interpretedA spoken or typed command was understood with AI (built-in phrases aren't announced) · userId
events.launchpad.technical.user.suspendedAn administrator suspended an account · userId, adminId
events.launchpad.technical.user.restoredAn administrator restored an account · userId, adminId
events.launchpad.technical.user.data_deletedAn administrator deleted an account's data · userId, adminId
events.launchpad.technical.catalog.access_grantedAn administrator gave someone an Android app or a browser extension of the catalog · userId, adminId, kind, package
events.launchpad.technical.catalog.access_revokedAn administrator took an Android app or a browser extension of the catalog back from someone · userId, adminId, kind, package
events.launchpad.technical.catalog.access_blockedAn administrator blocked someone from an app of the catalog, or from every app of it · userId, adminId, kind, package
events.launchpad.technical.catalog.access_unblockedAn administrator lifted a block on an app of the catalog, or on every app of it · userId, adminId, kind, package
events.launchpad.technical.invite.createdAn administrator issued an invite to Launcher, or a password link for an account · userId, inviteId, kind, adminId
events.launchpad.technical.invite.revokedAn administrator revoked an invite or a password link before it was used · userId, inviteId, kind, adminId
events.launchpad.technical.invite.acceptedSomeone joined Launcher with an invite, or set a new password with a password link · userId, inviteId, kind, adminId
dashboard-change-notices ← events.launchpad.business.dashboard.changed
Tells the person when an application changed their dashboard (once an hour per application) or an administrator replaced it
dashboard-link-notices ← events.launchpad.technical.dashboard.member_added
Tells the person an application can now change their dashboard
dashboard-unlink-notices ← events.launchpad.technical.dashboard.member_removed
Tells the person an application can no longer change their dashboard
A Node script to copy into your service or another app. It reads any app's events (EVENTS_APP), creates a durable consumer, retries a failed event, and stops cleanly; put your logic in handle().
#!/usr/bin/env node
/**
* A starter consumer for the events of any app built from the Starter
* template: the smallest service, or the first half of an integration between
* two apps, that reacts to them. Copy this file into your own service, install
* its two packages, and put your logic in handle().
*
* npm install @nats-io/transport-node @nats-io/jetstream
* NATS_URL=nats://<broker>:4222 NATS_USER=<user> NATS_PASSWORD=<password> \
* node events-consumer.mjs
*
* In this repo: pnpm events:consume (NATS_* from apps/api/.env).
*
* Every app publishes on the homelab's shared NATS account under its own
* name, events.<app>.<category>.<type>, into its own stream, EVENTS_<APP>.
*
* EVENTS_APP whose events to read; default launchpad. Its stream is
* EVENTS_<APP>: upper case, with - as _
* EVENTS_FILTER the subjects to take; default events.<app>.> (all of
* them), events.<app>.business.> for business events
* only, or one type such as
* events.launchpad.technical.user.signed_in
* EVENTS_CONSUMER the durable consumer's name; default events-consumer.
* Every app reading a stream shares its consumer names,
* so name it after your app and its job
* (homepage-key-auditor). It remembers its place, so a
* restart resumes where it stopped. A new name starts at
* the oldest event the stream holds, up to 7 days back.
* Remove one you no longer need with:
* nats consumer rm EVENTS_<APP> <name>
*
* Delivery is at least once and not always in order: handling one event
* twice must be harmless (its id is unique). Events carry ids, never content:
* read what you need through the publishing app's API, never its database.
*/
import {
AckPolicy,
DeliverPolicy,
JetStreamApiCodes,
JetStreamApiError,
jetstream,
jetstreamManager
} from "@nats-io/jetstream"
import { connect, nanos } from "@nats-io/transport-node"
const APP = process.env.EVENTS_APP || "launchpad"
const STREAM = `EVENTS_${APP.toUpperCase().replaceAll("-", "_")}`
const FILTER = process.env.EVENTS_FILTER || `events.${APP}.>`
const CONSUMER = process.env.EVENTS_CONSUMER || "events-consumer"
const MAX_DELIVER = 5
const RETRY_DELAY_MS = 10_000
/**
* Your logic. Throw to have the event delivered again 10 s later, up to 5
* deliveries in all.
*/
async function handle(event) {
console.log(`${event.occurredAt} ${event.type}`, JSON.stringify(event.data))
}
/** The event in a message, or null when it isn't one of APP's. */
function readEvent(message) {
try {
const event = JSON.parse(message.string())
return event?.app === APP && typeof event.type === "string"
? event
: null
} catch {
return null
}
}
function isApiError(error, code) {
return error instanceof JetStreamApiError && error.code === code
}
if (!process.env.NATS_URL) {
console.error(
"Set NATS_URL, and NATS_USER and NATS_PASSWORD when the broker asks for a login"
)
process.exit(1)
}
// Once connected, it reconnects forever, so a broker restart needs no restart
// here.
const nc = await connect({
servers: process.env.NATS_URL,
user: process.env.NATS_USER || undefined,
pass: process.env.NATS_PASSWORD || undefined,
name: CONSUMER,
maxReconnectAttempts: -1
}).catch((error) => {
console.error(
`Can't reach NATS at ${process.env.NATS_URL}: ${error.message}`
)
process.exit(1)
})
// Create the durable consumer, or point an existing one at FILTER; it keeps
// its place either way.
const jsm = await jetstreamManager(nc)
const settings = {
filter_subject: FILTER,
ack_wait: nanos(60_000),
max_deliver: MAX_DELIVER
}
try {
await jsm.consumers.info(STREAM, CONSUMER)
await jsm.consumers.update(STREAM, CONSUMER, settings)
} catch (error) {
if (isApiError(error, JetStreamApiCodes.StreamNotFound)) {
console.error(
`No ${STREAM} stream: ${APP} hasn't published to this broker yet, or this login is for another account`
)
await nc.close()
process.exit(1)
}
if (!isApiError(error, JetStreamApiCodes.ConsumerNotFound)) throw error
await jsm.consumers.add(STREAM, {
durable_name: CONSUMER,
ack_policy: AckPolicy.Explicit,
deliver_policy: DeliverPolicy.All,
...settings
})
}
const consumer = await jetstream(nc).consumers.get(STREAM, CONSUMER)
const messages = await consumer.consume()
for (const signal of ["SIGINT", "SIGTERM"]) {
process.once(signal, () => messages.close())
}
console.log(`[${CONSUMER}] consuming ${FILTER}; Ctrl+C stops`)
for await (const message of messages) {
const event = readEvent(message)
if (!event) {
console.error(
`skipped a message on ${message.subject}: not a ${APP} event`
)
message.term(`not a ${APP} event`)
continue
}
try {
await handle(event)
message.ack()
} catch (error) {
const attempt = message.info.deliveryCount
console.error(
`${event.id}: attempt ${attempt}/${MAX_DELIVER} failed`,
error
)
if (attempt >= MAX_DELIVER) message.term(`failed ${attempt} times`)
else message.nak(RETRY_DELAY_MS)
}
}
await nc.drain()