SDK: client
Receive events in your app over a WebSocket it opens itself, with filters, resume and reconnects.
Your app connects out to briefkasten, hosted or self-hosted, and receives the events it subscribes to. Nothing has to reach your server from the internet: it can sit behind Tailscale, a NAT or a firewall.
npm install briefkastenConnect
You need the gateway's URL and a subscriber token (briefkasten token create --name my-server).
import { connect } from "briefkasten";
const inbox = connect({
url: "https://briefkasten.example",
token: process.env.BRIEFKASTEN_TOKEN,
});
inbox.on("github:pull_request.opened", async (event) => {
await review(event.body.pull_request);
});connect() opens the WebSocket at once and keeps it open. Handlers run one event at a time, in the order the events arrived.
Filters
A pattern names a source and an event, with * for one part and ** for any number:
| Pattern | Matches |
|---|---|
github:push | push from the source github |
github:pull_request.* | every pull request event from github |
*:issue.created | issue.created from any source |
acme-shop:github:*:acme/shop:** | the full address: project, adapter, source, target, event |
Register as many handlers as you like; an event runs every handler that matches. To receive less over the wire, give the token filters when you create it, or pass filters to connect():
const inbox = connect({
url: "https://briefkasten.example",
token: process.env.BRIEFKASTEN_TOKEN,
filters: ["github:pull_request.*", "sentry:issue.*"],
});Resume and reconnects
Every event has an id that sorts by time. The client remembers the last one it handled and, after any disconnect, reconnects and asks for everything since then, so nothing is lost while your app restarts or deploys. Events are kept for your plan's retention (three days on the free plan).
To survive a restart of the process too, store the last id and pass it back:
const inbox = connect({
url: "https://briefkasten.example",
token: process.env.BRIEFKASTEN_TOKEN,
since: await store.get("briefkasten:last"),
onEvent: (event) => store.set("briefkasten:last", event.id),
});Delivery is at least once: after a reconnect an event can arrive twice. Its id never changes, so skipping one you have seen is enough.
Errors and shutting down
inbox.on("error", (error) => log.warn(error));
await inbox.close();A handler that throws is logged and the stream goes on; the event can be redelivered later. A wrong token ends the connection with unauthorized; anything else is retried with backoff.
See the library reference for every option.