MQTT

Configuration

Config structs, YAML examples, and option helpers for MQTT

YAML Configuration Example

config.yaml
extensions:
  mqtt:
    broker: "tcp://localhost:1883"
    clientID: "forge-mqtt-client"
    username: ""
    password: ""
    cleanSession: true
    connectTimeout: "30s"
    keepAlive: "60s"
    pingTimeout: "10s"
    maxReconnectDelay: "10m"

    # TLS
    enableTLS: false
    tlsCertFile: ""
    tlsKeyFile: ""
    tlsCAFile: ""

    # Messaging
    defaultQoS: 1                   # 0, 1, or 2
    autoReconnect: true
    resumeSubs: true
    maxReconnectAttempts: 0          # 0 = unlimited
    writeTimeout: "30s"
    messageChannelDepth: 100
    orderMatters: true

    # Message store
    messageStore: "memory"           # "memory" or "file"
    storeDirectory: ""

    # Last Will and Testament
    willEnabled: false
    willTopic: ""
    willPayload: ""
    willQoS: 0
    willRetained: false

    # Observability
    enableMetrics: true
    enableTracing: true
    enableLogging: true

The extension loads config from extensions.mqtt first, falling back to mqtt.

Programmatic Configuration

ext := mqtt.NewExtension(
    mqtt.WithBroker("tcp://broker.example.com:1883"),
    mqtt.WithClientID("my-iot-service"),
    mqtt.WithCredentials("user", "pass"),
    mqtt.WithQoS(1),
    mqtt.WithWill("devices/my-device/status", "offline", 1, true),
)

Config Struct Reference

Config

Source: config.go

FieldTypeDefaultDescription
Brokerstring"tcp://localhost:1883"Broker URL
ClientIDstring"forge-mqtt-client"Client identifier
Usernamestring""Auth username
Passwordstring""Auth password
CleanSessionbooltrueStart with a clean session
ConnectTimeouttime.Duration30sConnection timeout
KeepAlivetime.Duration60sKeepalive interval
PingTimeouttime.Duration10sPing response timeout
MaxReconnectDelaytime.Duration10mMax reconnection backoff
EnableTLSboolfalseEnable TLS
DefaultQoSbyte1Default QoS level (0, 1, 2)
AutoReconnectbooltrueAuto-reconnect on disconnect
ResumeSubsbooltrueRe-subscribe after reconnect
MaxReconnectAttemptsint0Max reconnect attempts (0 = unlimited)
WriteTimeouttime.Duration30sWrite timeout
MessageChannelDepthuint100Internal message channel buffer
OrderMattersbooltrueEnforce message ordering
MessageStorestring"memory"Message store: "memory" or "file"
StoreDirectorystring""Directory for file-based store
WillEnabledboolfalseEnable Last Will
WillTopicstring""Will topic
WillPayloadstring""Will message
WillQoSbyte0Will QoS level
WillRetainedboolfalseWill retained flag
EnableMetricsbooltrueEnable metrics
EnableTracingbooltrueEnable tracing
EnableLoggingbooltrueEnable logging

Option Helpers

FunctionDescription
WithBroker(url)Set broker URL
WithClientID(id)Set client identifier
WithCredentials(username, password)Set authentication credentials
WithTLS(certFile, keyFile, caFile)Enable TLS
WithQoS(qos)Set default QoS level
WithCleanSession(clean)Toggle clean sessions
WithKeepAlive(interval)Set keepalive interval
WithAutoReconnect(enabled)Toggle auto-reconnect
WithWill(topic, payload, qos, retained)Configure Last Will and Testament
WithMetrics(enabled)Toggle metrics
WithTracing(enabled)Toggle tracing
WithRequireConfig(require)Require config from ConfigManager
WithConfig(config)Provide a complete config struct

How is this guide?

On this page