MQTT

Functions

Public API reference for the MQTT extension

Extension Entry Points

FunctionDescription
NewExtension(opts ...ConfigOption) forge.ExtensionCreate the MQTT extension with functional options
NewExtensionWithConfig(config Config) forge.ExtensionCreate the MQTT extension with a complete config
DefaultConfig() ConfigReturns the default configuration

DI Helpers

FunctionDescription
GetMQTT(c forge.Container) (MQTT, error)Resolve the MQTT client from the container
MustGetMQTT(c forge.Container) MQTTSame but panics on error
GetMQTTFromApp(app forge.App) (MQTT, error)Resolve from an app instance
MustGetMQTTFromApp(app forge.App) MQTTSame but panics on error

MQTT Interface -- Connection

MethodDescription
Connect(ctx) errorConnect to the MQTT broker
Disconnect(ctx) errorDisconnect gracefully (250ms quiesce)
Ping(ctx) errorVerify broker connectivity
IsConnected() boolCheck if currently connected

MQTT Interface -- Publishing

MethodDescription
Publish(ctx, topic, payload, qos, retained) errorPublish a message to a topic

MQTT Interface -- Subscribing

MethodDescription
Subscribe(ctx, topic, qos, handler) errorSubscribe to a topic with a message handler
SubscribeMultiple(ctx, subscriptions) errorSubscribe to multiple topics at once
Unsubscribe(ctx, topics...) errorUnsubscribe from one or more topics
GetSubscriptions() []SubscriptionInfoList active subscriptions

MQTT Interface -- Connection Handlers

MethodDescription
SetOnConnect(handler)Set callback for successful connection
SetOnConnectionLost(handler)Set callback for connection loss
SetOnReconnecting(handler)Set callback when reconnection starts

MQTT Interface -- Stats

MethodDescription
Stats() ClientStatsGet client statistics

Key Types

SubscriptionInfo

FieldTypePurpose
TopicstringSubscribed topic pattern
QoSbyteQoS level for this subscription

ClientStats

FieldTypePurpose
ConnectedboolCurrent connection status
MessagesPublishedint64Total messages published
MessagesReceivedint64Total messages received
SubscriptionsintNumber of active subscriptions
Reconnectionsint64Number of reconnection attempts

How is this guide?

On this page