Realm event in. Browser notification out.
Web Push for gno.land dapps: users who opt in get a browser notification when your realm emits an event, even with the tab closed.
Add it to your dapp Try the demo How it works ↓
- No server to run: the public server sends the notifications, on the onyx testnet, version 0.
- Works in browsers with Web Push, such as Chrome. On iPhone and iPad, only for sites added to the Home Screen.
TurnPlayed game=0000042 next=g1k7…x2q turn=7
Mentioned channel=general by=g1zm…9ra who=g1k7…x2q
ProposalClosed id=0000012 result=passed
Outbid lot=0000309 by=g1p0…7cd was=g1k7…x2q
Granted account=g1k7…x2q unlock=1300000
How it works
-
Emit an event from your realm
A realm with one function,
Step 1 in Getting started →Notify, emits a message and the address it is for.gnofunc Notify(cur realm, to address, msg string) { chain.Emit("Message", "to", to.String(), "msg", msg) } -
Set a trigger in the registry
The realm opens its handle once, in
Step 2 in Getting started →init, and sets its trigger: which event to watch, who hears it, and what the notification says.gnoimport "gno.land/r/nym-gfanton001/gnotif/v1" func init(cur realm) { me := cur.Sub(gnotif.Subpath) h := gnotif.Open(cross(me)) h.Set(cross(me), "message", gnotif.Trigger{ Event: "Message", Param: "to", Title: "New message", Body: "{msg}", Link: "/", }) } -
Subscribe the browser from your page
Install the client and serve its
Step 3 in Getting started →sw.jsat your site's root. From a click, callenable(), then opt the browser in with the user's address. Callconnect()too, and an open page gets each one live.shnpm install gnotif cp node_modules/gnotif/dist/sw.js public/sw.jsjsimport { Gnotif } from "gnotif"; const gnotif = new Gnotif({ network: "onyx" }); // <you>: the address you deployed the realm from const [message] = await gnotif.triggers("gno.land/r/<you>/notify"); if (!message) throw new Error("gnotif.xyz has not read this realm's trigger yet"); // button: a button on your page; address: the user's gno.land address button.addEventListener("click", async () => { await gnotif.enable(); await gnotif.setOptins([{ trigger: message.id, value: address }]); });
Explore the delivery path
- dapp realmemits events, declares its trigger
- gnotif registryholds the triggers, on chain
- tx-indexerreads the chain, serves GraphQL
- gnotifdmatches events, sends the pushes
- push serviceWeb Push, run by the browser's vendor
- sw.jsshows the notification, on the dapp's origin
Dashed: yours, in your dapp. Solid: run by gnotif, the chain and the browser’s vendor.
The notification travels through the browser’s push service to the service worker on your dapp’s origin.
Add it to your dapp Try the demo
Want your own server? gnotifd is one binary with one SQLite file. Running gnotifd →