Add Web Push Notifications
Send web push from a Worker with VAPID, prune dead subscriptions, and report errors to Sentry.
Install
npm i @dapplelight/signet-serverCreate the pusher
import { createPusher, webCryptoPushSender } from "@dapplelight/signet-server/notify/push"
import { db } from "./db"
import type { Env } from "./env"
export const pusherFor = (env: Env) =>
createPusher({
send: webCryptoPushSender({
vapid: { subject: "mailto:[email protected]", publicKey: env.VAPID_PUBLIC, privateKey: env.VAPID_PRIVATE },
}),
removeSubscription: (id) => db.deleteSubscription(id),
})The public key is the 65-byte point you give pushManager.subscribe. Both are base64url.
Send
import { app } from "../app"
import { subscriptions } from "../db"
import { pusherFor } from "../push"
app.post("/orders/ord_1042/ship", async (c) => {
const sent = await pusherFor(c.env).sendTo(subscriptions, {
title: "Order shipped",
message: "Your order ord_1042 is on its way.",
url: "/orders/ord_1042",
})
return c.json({ sent })
})sendTo returns the count delivered and never throws. A 404 or 410 from the endpoint removes that subscription.
Add email and operator alerts
import { createEmailer } from "@dapplelight/signet-server/notify/email"
import { createTelegram } from "@dapplelight/signet-server/notify/telegram"Telegram is operator-level only: never put user names, amounts or account ids in it.