PathrockNetwork Gno Explorer
HomeBlocksTransactionsTokensRealmsPackagesValidatorsAnalytics

PathrockNetwork Gno Explorer — an independent explorer for Gno.land Mainnet (gnoland-1), operated by PathrockNetwork. Not an official Gno.land service.

gnowebarchive RPC

gno.land/p/g17khqpukees4237dtn3astzapmp462vjhsz6st4/message

Package
Open in gnoweb ↗

Overview

Kind
Pure package
Name
message
Namespace
g17khqpukees4237dtn3astzapmp462vjhsz6st4
Files
4 (README)(gnomod.toml)
Exported functions
n/a — not supported for pure packages by the node (vm/qfuncs)
Module
gno.land/p/g17khqpukees4237dtn3astzapmp462vjhsz6st4/message
gno
0.9

Files (4)

  • README.mdmarkdown
  • gnomod.tomltoml
  • broker.gnogno
  • message.gnogno
README.mdPreviewRaw
# Message Package

Package provides a simple message broker implementation.

The message broker is a Pub/Sub one. It implements two different interfaces,
`Publisher` and `Subscriber`, which are also defined within this package.

Published messages contain the topic where they are published and optional
message data. Subscribing to the `TopicAll` topic triggers the callback for
messages published to any topic.

Repository can be found at [jeronimoalbi/gnome](https://github.com/jeronimoalbi/gnome),
as part of `jeronimoalbi`'s Gno smart contracts monorepo.

## Usage

[embedmd]:# (filetests/readme_filetest.gno go)
```go
package main

import "gno.land/p/jeronimoalbi/message"

func main() {
	broker := message.NewBroker()

	// Subscribe to an event
	subID, err := broker.Subscribe("EventName", func(msg message.Message) {
		println("EventName has been triggered:", msg.Data.(string))
	})
	if err != nil {
		panic(err)
	}

	// Publish an event
	err = broker.Publish("EventName", "Example event data")
	if err != nil {
		panic(err)
	}

	// Unsubscribe from the event
	unsubscribed, err := broker.Unsubscribe("EventName", subID)
	if err != nil {
		panic(err)
	}

	if !unsubscribed {
		panic("subscription not found")
	}

	// Nothing is triggered after unsubscribing
	err = broker.Publish("EventName", "Ignored event data")
	if err != nil {
		panic(err)
	}
}

// Output:
// EventName has been triggered: Example event data
```

Functions

not supported for pure packages by the node (vm/qfuncs)

Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.