Forge
1.x
Docs/Forge/Functions
Open

Reading1 min
Updated31 Jul 2026
Sourcev1/extensions/mqtt/functions.mdx

Extension Entry Points01

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 Helpers02

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 -- Connection03

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 -- Publishing04

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

MQTT Interface -- Subscribing05

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 Handlers06

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

MQTT Interface -- Stats07

MethodDescription
Stats() ClientStatsGet client statistics

Key Types08

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