Mqtt Config
Configuration for an MQTT 5.0 client connection.
Maps to the fields and properties of the CONNECT packet (§3.1). All binary data uses ByteString for immutability. Construct using named parameters and Kotlin's copy() for immutable configuration variants.
Example
val config = MqttConfig(
clientId = "sensor-hub-01",
keepAliveSeconds = 30,
cleanStart = false,
autoReconnect = true,
defaultQos = QoS.AT_LEAST_ONCE,
)Or using the builder DSL:
val config = MqttConfig.build {
clientId = "sensor-hub-01"
keepAliveSeconds = 30
defaultQos = QoS.AT_LEAST_ONCE
will {
topic = "sensors/status"
payload("offline")
retain = true
}
}Constructors
Types
Properties
Initial authentication data for enhanced auth (§3.1.2.11.10). The format is defined by the authenticationMethod.
Authentication method for MQTT 5.0 enhanced authentication (§4.12), e.g. "SCRAM-SHA-256". When set, enables the AUTH packet challenge/response flow.
If true (default), the client automatically attempts to reconnect with exponential backoff when the connection is lost unexpectedly. Subscriptions are re-established on successful reconnection.
If true, the broker discards any existing session state and starts fresh. If false, the broker resumes the previous session (identified by clientId) including in-flight QoS 1/2 messages and active subscriptions (§3.1.2.4). Note: Client-side session persistence is not yet implemented. When cleanStart=false, the broker will resume session state but the client does not persist in-flight QoS 1/2 messages across reconnects. Packet IDs are preserved, but unacknowledged messages may be lost. Full session persistence will be added in a future release.
Default QoS level applied to convenience MqttClient.publish calls when no explicit QoS is specified. Similar to Ktor's defaultRequest {} pattern. Defaults to QoS.AT_MOST_ONCE.
Default retain flag applied to convenience MqttClient.publish calls when no explicit retain value is specified. Defaults to false.
Maximum interval in seconds between control packets (§3.1.2.10). The client sends PINGREQ if no other packet is sent within 75% of this interval. Set to 0 to disable the keepalive mechanism. Range: 0..65,535.
Optional MqttLogger implementation that receives log messages from the library. When null (default), no logging occurs and zero overhead is incurred.
Minimum log level for messages delivered to logger. Messages below this level are discarded without constructing the message string. Defaults to MqttLogLevel.NONE (all logging disabled).
Maximum MQTT packet size (in bytes) the client can accept. null = no limit imposed. Range: 1..4,294,967,295 (§3.1.2.11.5).
Maximum number of consecutive reconnection attempts before giving up. 0 = unlimited attempts. Defaults to 0 (unlimited).
Maximum number of concurrent in-flight QoS 1 and QoS 2 messages the client is willing to process. The broker will not send more than this number of unacknowledged publishes. Range: 1..65,535 (§3.1.2.11.4).
Initial delay between reconnection attempts in milliseconds. Doubles after each failed attempt up to reconnectMaxDelayMs. Must be > 0.
Maximum delay between reconnection attempts in milliseconds. Must be ≥ reconnectBaseDelayMs.
If true (default), the broker may include Reason String and User Properties in packets where a reason code indicates failure (§3.1.2.11.8).
If true, requests the broker to include Response Information in CONNACK, which can be used as a basis for response topics (§3.1.2.11.7).
How long the broker retains session state after disconnection, in seconds. 0 = expire immediately on disconnect, null = use broker default. Max value: 4,294,967,295 (§3.1.2.11.3).
Maximum number of topic aliases the client accepts from the broker. 0 = topic aliases are not supported by this client. Range: 0..65,535 (§3.1.2.11.6).
Application-defined key-value string pairs sent with the CONNECT packet. Keys may repeat. Forwarded to the broker but not to other clients.
Optional will message configuration. If set, the broker publishes this message when the client disconnects unexpectedly (§3.1.3.2).