messaging

package
v0.43.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

Messaging

messaging package defines Publisher, Subscriber and an aggregate Pubsub interface.

Subscriber interface defines methods used to subscribe to a message broker such as MQTT or NATS or RabbitMQ.

Publisher interface defines methods used to publish messages to a message broker such as MQTT or NATS or RabbitMQ.

Pubsub interface is composed of Publisher and Subscriber interface and can be used to send messages to as well as to receive messages from a message broker.

Documentation

Index

Constants

View Source
const (
	SenMLContentType = "application/senml+json"
	JSONContentType  = "application/json"
)

Variables

View Source
var (
	// ErrPublishMessage indicates that message publishing failed.
	ErrPublishMessage = errors.New("failed to publish message")

	// ErrPublishCommand indicates that command publishing failed.
	ErrPublishCommand = errors.New("failed to publish command")

	// ErrPublishAlarm indicates that alarm publishing failed.
	ErrPublishAlarm = errors.New("failed to publish alarm")

	// ErrPublishNotification indicates that notification publishing failed.
	ErrPublishNotification = errors.New("failed to publish notification")

	// ErrPublishWebhook indicates that webhook publishing failed.
	ErrPublishWebhook = errors.New("failed to publish webhook")

	// ErrMalformedSubtopic indicates that the subtopic is malformed.
	ErrMalformedSubtopic = errors.New("malformed subtopic")
)
View Source
var (
	// ErrFailedSubscribe indicates that subscribing to a topic failed.
	ErrFailedSubscribe = errors.New("failed to subscribe")

	// ErrFailedUnsubscribe indicates that unsubscribing from a topic failed.
	ErrFailedUnsubscribe = errors.New("failed to unsubscribe")

	// ErrSubscribeTimeout indicates that the subscription failed due to timeout.
	ErrSubscribeTimeout = errors.New("failed to subscribe due to timeout reached")

	// ErrUnsubscribeTimeout indicates that unsubscribe failed due to timeout.
	ErrUnsubscribeTimeout = errors.New("failed to unsubscribe due to timeout reached")

	// ErrUnsubscribeDeleteTopic indicates that unsubscribe failed because the topic was deleted.
	ErrUnsubscribeDeleteTopic = errors.New("failed to unsubscribe due to deletion of topic")

	// ErrNotSubscribed indicates that the topic is not subscribed to.
	ErrNotSubscribed = errors.New("not subscribed")

	// ErrEmptyTopic indicates the absence of topic.
	ErrEmptyTopic = errors.New("empty topic")

	// ErrEmptyID indicates the absence of ID.
	ErrEmptyID = errors.New("empty ID")
)
View Source
var ErrInvalidContentType = errors.New("invalid content type")

ErrInvalidContentType indicates an invalid Content-Type

Functions

func FormatMessage added in v0.26.0

func FormatMessage(pc domain.PubConfigInfo, msg *protomfx.Message) error

func NormalizeSubtopic added in v0.35.0

func NormalizeSubtopic(topic string) (string, error)

NormalizeSubtopic decodes a client-supplied subtopic and converts it into the dot-separated form used in subjects, rejecting any element containing a NATS wildcard token (* or >) so a subtopic can't smuggle one in.

func PayloadHash added in v0.42.0

func PayloadHash(payload []byte) int32

func SplitMessage added in v0.28.0

func SplitMessage(message protomfx.Message) ([]protomfx.Message, error)

func ToJSONCommand added in v0.42.0

func ToJSONCommand(cmd protomfx.Command) mfjson.Command

func ToJSONMessage added in v0.28.0

func ToJSONMessage(message protomfx.Message) mfjson.Message

func ToSenMLMessage added in v0.28.0

func ToSenMLMessage(message protomfx.Message) (senml.Message, error)

Types

type AlarmHandler added in v0.40.0

type AlarmHandler func(subject string, alarm protomfx.Alarm) error

AlarmHandler handles a received protomfx.Alarm for AlarmSubscriber.

type AlarmPublisher added in v0.40.0

type AlarmPublisher interface {
	// PublishAlarm publishes an alarm to the message broker.
	PublishAlarm(subject string, alarm protomfx.Alarm) error
}

AlarmPublisher specifies the alarm publishing API.

type AlarmSubscriber added in v0.40.0

type AlarmSubscriber interface {
	// SubscribeAlarms subscribes to the alarm stream.
	SubscribeAlarms(id string, handler AlarmHandler) error
}

AlarmSubscriber specifies the alarm subscription API.

type Canceler added in v0.43.0

type Canceler interface {
	Cancel() error
}

Canceler is an optional capability a MessageHandler may implement for cleanup when its subscription ends. Subscriber implementations detect it via a type assertion.

type CommandHandler added in v0.41.0

type CommandHandler func(subject string, cmd protomfx.Command) error

CommandHandler handles a received protomfx.Command for CommandSubscriber.

type CommandPublisher added in v0.41.0

type CommandPublisher interface {
	// PublishCommand publishes a command to the message broker.
	PublishCommand(subject string, cmd protomfx.Command) error
}

CommandPublisher specifies the command publishing API.

type CommandSubscriber added in v0.41.0

type CommandSubscriber interface {
	// SubscribeCommands subscribes to the command stream for the given topic.
	SubscribeCommands(id, topic string, handler CommandHandler) error
}

CommandSubscriber specifies the command subscription API.

type MessageDispatcher added in v0.42.0

type MessageDispatcher interface {
	// Dispatch publishes msg to every subject enabled by the dispatcher flags in pc.
	Dispatch(msg protomfx.Message, pc *domain.ProfileConfig) error
}

MessageDispatcher routes a message to every subject enabled by a profile's dispatcher flags.

type MessageHandler

type MessageHandler interface {
	// Handle handles messages passed by underlying implementation.
	Handle(subject string, msg protomfx.Message) error
}

MessageHandler represents protomfx.Message handler for Subscriber.

type NotificationHandler added in v0.42.0

type NotificationHandler func(subject string, notification protomfx.Notification) error

NotificationHandler handles a received protomfx.Notification for NotificationSubscriber.

type NotificationPublisher added in v0.42.0

type NotificationPublisher interface {
	// PublishNotification publishes a notification to the message broker.
	PublishNotification(subject string, notification protomfx.Notification) error
}

NotificationPublisher specifies the notification publishing API.

type NotificationSubscriber added in v0.42.0

type NotificationSubscriber interface {
	// SubscribeNotifications subscribes to the notification stream for the given topic.
	SubscribeNotifications(id, topic string, handler NotificationHandler) error
}

NotificationSubscriber specifies the notification subscription API.

type PubSub

type PubSub interface {
	Publisher
	Subscriber
}

PubSub represents aggregation interface for publisher and subscriber.

type Publisher

type Publisher interface {
	// Publish publishes message to the message broker.
	Publish(subject string, msg protomfx.Message) error

	// Close gracefully closes message publisher's connection.
	Close() error
}

Publisher specifies message publishing API.

type Subscriber

type Subscriber interface {
	// Subscribe subscribes to the message stream and consumes messages.
	Subscribe(id, topic string, handler MessageHandler) error

	// Unsubscribe unsubscribes from the message stream and
	// stops consuming messages.
	Unsubscribe(id, topic string) error

	// Close gracefully closes message subscriber's connection.
	Close() error
}

Subscriber specifies message subscription API.

type WebhookHandler added in v0.42.0

type WebhookHandler func(subject string, webhook protomfx.Webhook) error

WebhookHandler handles a received protomfx.Webhook for WebhookSubscriber.

type WebhookPublisher added in v0.42.0

type WebhookPublisher interface {
	// PublishWebhook publishes a webhook message to the message broker.
	PublishWebhook(subject string, webhook protomfx.Webhook) error
}

WebhookPublisher specifies the webhook publishing API.

type WebhookSubscriber added in v0.42.0

type WebhookSubscriber interface {
	// SubscribeWebhooks subscribes to the webhook stream.
	SubscribeWebhooks(id string, handler WebhookHandler) error
}

WebhookSubscriber specifies the webhook subscription API.

Directories

Path Synopsis
Package mqtt hold the implementation of the Publisher and PubSub interfaces for the MQTT messaging system, the internal messaging broker of the Mainflux IoT platform.
Package mqtt hold the implementation of the Publisher and PubSub interfaces for the MQTT messaging system, the internal messaging broker of the Mainflux IoT platform.
Package nats implements messaging.Publisher and messaging.Subscriber (plus the typed Alarm/Command/Notification/Webhook streams) for NATS JetStream, the internal message broker of the Mainflux IoT platform.
Package nats implements messaging.Publisher and messaging.Subscriber (plus the typed Alarm/Command/Notification/Webhook streams) for NATS JetStream, the internal message broker of the Mainflux IoT platform.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL