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.
pingpong · TurnPlayed
TurnPlayed game=0000042 next=g1k7⁠…⁠x2q turn=7
notification · the player's browser
Your turnGame 0000042, turn 7pingpong · just now
gnochat · Mentioned
Mentioned channel=general by=g1zm⁠…⁠9ra who=g1k7⁠…⁠x2q
notification · the member's browser
g1zm⁠…⁠9ra mentioned youin #generalgnochat · just now
govdao · ProposalClosed
ProposalClosed id=0000012 result=passed
notification · every voter's browser
Proposal 12 passedTreasury budget, Q4govdao · just now
auctions · Outbid
Outbid lot=0000309 by=g1p0⁠…⁠7cd was=g1k7⁠…⁠x2q
notification · the bidder's browser
You were outbidLot 309, now 1 250 GNOTauctions · just now
vesting · Granted
Granted account=g1k7⁠…⁠x2q unlock=1300000
notification · the grantee's browser
New vesting grant10 000 GNOT, unlocking at block 1 300 000vesting · just now

How it works

  1. Emit an event from your realm

    A realm with one function, Notify, emits a message and the address it is for.

    Step 1 in Getting started →
    gno
    func Notify(cur realm, to address, msg string) {
    	chain.Emit("Message", "to", to.String(), "msg", msg)
    }
  2. Set a trigger in the registry

    The realm opens its handle once, in init, and sets its trigger: which event to watch, who hears it, and what the notification says.

    Step 2 in Getting started →
    gno
    import "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: "/",
    	})
    }
  3. Subscribe the browser from your page

    Install the client and serve its sw.js at your site's root. From a click, call enable(), then opt the browser in with the user's address. Call connect() too, and an open page gets each one live.

    Step 3 in Getting started →
    sh
    npm install gnotif
    cp node_modules/gnotif/dist/sw.js public/sw.js
    js
    import { 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
  1. dapp realmemits events, declares its trigger
  2. gnotif registryholds the triggers, on chain
  3. tx-indexerreads the chain, serves GraphQL
  4. gnotifdmatches events, sends the pushes
  5. push serviceWeb Push, run by the browser's vendor
  6. 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 →