MQTT
Functions
Public API reference for the MQTT extension
| Function | Description |
|---|
NewExtension(opts ...ConfigOption) forge.Extension | Create the MQTT extension with functional options |
NewExtensionWithConfig(config Config) forge.Extension | Create the MQTT extension with a complete config |
DefaultConfig() Config | Returns the default configuration |
| Function | Description |
|---|
GetMQTT(c forge.Container) (MQTT, error) | Resolve the MQTT client from the container |
MustGetMQTT(c forge.Container) MQTT | Same but panics on error |
GetMQTTFromApp(app forge.App) (MQTT, error) | Resolve from an app instance |
MustGetMQTTFromApp(app forge.App) MQTT | Same but panics on error |
| Method | Description |
|---|
Connect(ctx) error | Connect to the MQTT broker |
Disconnect(ctx) error | Disconnect gracefully (250ms quiesce) |
Ping(ctx) error | Verify broker connectivity |
IsConnected() bool | Check if currently connected |
| Method | Description |
|---|
Publish(ctx, topic, payload, qos, retained) error | Publish a message to a topic |
| Method | Description |
|---|
Subscribe(ctx, topic, qos, handler) error | Subscribe to a topic with a message handler |
SubscribeMultiple(ctx, subscriptions) error | Subscribe to multiple topics at once |
Unsubscribe(ctx, topics...) error | Unsubscribe from one or more topics |
GetSubscriptions() []SubscriptionInfo | List active subscriptions |
| Method | Description |
|---|
SetOnConnect(handler) | Set callback for successful connection |
SetOnConnectionLost(handler) | Set callback for connection loss |
SetOnReconnecting(handler) | Set callback when reconnection starts |
| Method | Description |
|---|
Stats() ClientStats | Get client statistics |
| Field | Type | Purpose |
|---|
Topic | string | Subscribed topic pattern |
QoS | byte | QoS level for this subscription |
| Field | Type | Purpose |
|---|
Connected | bool | Current connection status |
MessagesPublished | int64 | Total messages published |
MessagesReceived | int64 | Total messages received |
Subscriptions | int | Number of active subscriptions |
Reconnections | int64 | Number of reconnection attempts |
How is this guide?