const TopicAll
TopicAll defines a topic for all types of message. This topic can be used to subscribe to message for all topics.
Package message provides a simple message broker implementation.
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.
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
Package message provides a simple message broker implementation.
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)Broker is a message broker that handles subscriptions and message publishing.
Publish publishes a message for a topic.
Subscribe subscribes to messages published for a topic. It returns the callback ID within the topic.
Topics returns the list of current subscription topics.
Unsubscribe unsubscribes a callback from a message topic. ID is the callback ID within the topic, returned on subscription.
Callback defines a type for message callbacks.
Message defines a type for published messages.
Publisher defines an interface for message publishers.
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}Subscriber defines an interface for message subscribers.
Topic defines a type for message topics.