Documentation
¶
Overview ¶
Package messaging implements messaging patterns like publish-subscribe to decouple local and remote services.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher interface {
Publish(ctx context.Context, message Message) error
Subscribe(ctx context.Context, topic string, fn service.Function[Message, MessageState]) error
}
Dispatcher is an interface for a message dispatcher.
func NewExternalDispatcher ¶ added in v0.1.82
func NewExternalDispatcher() Dispatcher
NewExternalDispatcher creates a new externalDispatcher instance.
func NewInternalDispatcher ¶ added in v0.1.79
func NewInternalDispatcher() Dispatcher
NewInternalDispatcher creates a new internalDispatcher instance.
Example ¶
package main
import (
"context"
"fmt"
"github.com/andygeiss/cloud-native-utils/messaging"
"github.com/andygeiss/cloud-native-utils/service"
)
func main() {
dispatcher := messaging.NewInternalDispatcher()
ctx := context.Background()
_ = dispatcher.Subscribe(ctx, "user.created", service.Wrap(func(m messaging.Message) (messaging.MessageState, error) {
fmt.Printf("%s: %s\n", m.Topic, m.Data)
return messaging.MessageStateCompleted, nil
}))
// Publish returns once every subscriber has finished.
_ = dispatcher.Publish(ctx, messaging.NewMessage("user.created", []byte("alice")))
}
Output: user.created: alice
type Message ¶
type Message struct {
Topic string `json:"topic"`
Data []byte `json:"data"`
State MessageState `json:"state"`
}
Message is a struct that represents a message.
func NewMessage ¶
NewMessage creates a new message.
type MessageState ¶ added in v0.1.79
type MessageState int
MessageState is an enum that represents the state of a message.
const ( MessageStateCreated MessageState = iota MessageStateCompleted MessageStateFailed )
Click to show internal directories.
Click to hide internal directories.