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.

Overview

Package message 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.

Subscribe to an event:

Example
1broker := message.NewBroker()
2subID, err := broker.Subscribe("EventName", func(msg message.Message) {
3   println("EventName has been triggered")
4   println(msg.Data)
5})
6if err != nil {
7   panic(err)
8}

Unsubscribe from an event:

Example
1unsubscribed, err := broker.Unsubscribe("EventName", subID)
2if err != nil {
3   panic(err)
4}
5
6if !unsubscribed {
7   panic("subscription not found")
8}

Publish an event:

Example
1err := broker.Publish("EventName", "Example event data")
2if err != nil {
3   panic(err)
4}

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 avl.Tree // 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 6