Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

message source pure

Package message provides a simple message broker implementation.

Readme View source

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, as part of jeronimoalbi's Gno smart contracts monorepo.

Usage

 1package main
 2
 3import "gno.land/p/jeronimoalbi/message"
 4
 5func main() {
 6	broker := message.NewBroker()
 7
 8	// Subscribe to an event
 9	subID, err := broker.Subscribe("EventName", func(msg message.Message) {
10		println("EventName has been triggered:", msg.Data.(string))
11	})
12	if err != nil {
13		panic(err)
14	}
15
16	// Publish an event
17	err = broker.Publish("EventName", "Example event data")
18	if err != nil {
19		panic(err)
20	}
21
22	// Unsubscribe from the event
23	unsubscribed, err := broker.Unsubscribe("EventName", subID)
24	if err != nil {
25		panic(err)
26	}
27
28	if !unsubscribed {
29		panic("subscription not found")
30	}
31
32	// Nothing is triggered after unsubscribing
33	err = broker.Publish("EventName", "Ignored event data")
34	if err != nil {
35		panic(err)
36	}
37}
38
39// Output:
40// EventName has been triggered: Example event data

Overview

Package message provides a simple message broker implementation.

Constants 1

const TopicAll

1const TopicAll Topic = "*"
source

TopicAll defines a topic for all types of message. This topic can be used to subscribe to message for all topics.

Variables 1

var ErrInvalidTopic, ErrRequiredCallback, ErrRequiredSubscriptionID, ErrRequiredTopic

 1var (
 2	// ErrInvalidTopic is triggered when an invalid topic is used.
 3	ErrInvalidTopic = errors.New("invalid topic")
 4
 5	// ErrRequiredCallback is triggered when subscribing without a callback.
 6	ErrRequiredCallback = errors.New("message callback is required")
 7
 8	// ErrRequiredSubscriptionID is triggered when unsubscribing without an ID.
 9	ErrRequiredSubscriptionID = errors.New("message sibscription ID is required")
10
11	// ErrRequiredTopic is triggered when (un)subscribing without a topic.
12	ErrRequiredTopic = errors.New("message topic is required")
13)
source

Functions 1

func NewBroker

1func NewBroker() *Broker
source

NewBroker creates a new message broker.

Types 6

type Broker

struct
1type Broker struct {
2	callbacks *bptree.BPTree // string(topic) -> *ulist.List(Callback)
3}
source

Broker is a message broker that handles subscriptions and message publishing.

Methods on Broker

func Publish

method on Broker
1func (b *Broker) Publish(topic Topic, data any) error
source

Publish publishes a message for a topic.

func Subscribe

method on Broker
1func (b *Broker) Subscribe(topic Topic, cb Callback) (id int, _ error)
source

Subscribe subscribes to messages published for a topic. It returns the callback ID within the topic.

func Topics

method on Broker
1func (b *Broker) Topics() []Topic
source

Topics returns the list of current subscription topics.

func Unsubscribe

method on Broker
1func (b *Broker) Unsubscribe(topic Topic, id int) (unsubscribed bool, _ error)
source

Unsubscribe unsubscribes a callback from a message topic. ID is the callback ID within the topic, returned on subscription.

type Callback

func
1type Callback func(Message)
source

Callback defines a type for message callbacks.

type Message

struct
1type Message struct {
2	// Topic is the message topic.
3	Topic Topic
4
5	// Data contains optional message data.
6	Data any
7}
source

Message defines a type for published messages.

type Publisher

interface
1type Publisher interface {
2	// Publish publishes a message for a topic.
3	Publish(_ Topic, data any) error
4}
source

Publisher defines an interface for message publishers.

type Subscriber

interface
1type Subscriber interface {
2	// Subscribe subscribes to messages published for a topic.
3	// It returns the callback ID within the topic.
4	Subscribe(Topic, Callback) (id int, _ error)
5
6	// Unsubscribe unsubscribes a callback from a message topic.
7	// ID is the callback ID within the topic, returned on subscription.
8	Unsubscribe(_ Topic, id int) (unsubscribed bool, _ error)
9}
source

Subscriber defines an interface for message subscribers.

type Topic

ident
1type Topic string
source

Topic defines a type for message topics.

Imports 4

Source Files 4