MqttConfig

data class MqttConfig(val clientId: String = "", val keepAliveSeconds: Int = 60, val cleanStart: Boolean = true, val username: String? = null, val password: ByteString? = null, val will: WillConfig? = null, val sessionExpiryInterval: Long? = null, val receiveMaximum: Int = 65535, val maximumPacketSize: Long? = null, val topicAliasMaximum: Int = 0, val requestResponseInformation: Boolean = false, val requestProblemInformation: Boolean = true, val userProperties: List<Pair<String, String>> = emptyList(), val authenticationMethod: String? = null, val authenticationData: ByteString? = null, val autoReconnect: Boolean = true, val reconnectBaseDelayMs: Long = 1000, val reconnectMaxDelayMs: Long = 30000, val maxReconnectAttempts: Int = 0, val defaultQos: QoS = QoS.AT_MOST_ONCE, val defaultRetain: Boolean = false, val logger: MqttLogger? = null, val logLevel: MqttLogLevel = MqttLogLevel.NONE)

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

Link copied to clipboard
constructor(clientId: String = "", keepAliveSeconds: Int = 60, cleanStart: Boolean = true, username: String? = null, password: ByteString? = null, will: WillConfig? = null, sessionExpiryInterval: Long? = null, receiveMaximum: Int = 65535, maximumPacketSize: Long? = null, topicAliasMaximum: Int = 0, requestResponseInformation: Boolean = false, requestProblemInformation: Boolean = true, userProperties: List<Pair<String, String>> = emptyList(), authenticationMethod: String? = null, authenticationData: ByteString? = null, autoReconnect: Boolean = true, reconnectBaseDelayMs: Long = 1000, reconnectMaxDelayMs: Long = 30000, maxReconnectAttempts: Int = 0, defaultQos: QoS = QoS.AT_MOST_ONCE, defaultRetain: Boolean = false, logger: MqttLogger? = null, logLevel: MqttLogLevel = MqttLogLevel.NONE)

Types

Link copied to clipboard
class Builder

Mutable builder for MqttConfig.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val authenticationData: ByteString?

Initial authentication data for enhanced auth (§3.1.2.11.10). The format is defined by the authenticationMethod.

Link copied to clipboard

Authentication method for MQTT 5.0 enhanced authentication (§4.12), e.g. "SCRAM-SHA-256". When set, enables the AUTH packet challenge/response flow.

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard

Client identifier sent to the broker. An empty string requests the broker to assign a unique identifier (returned in CONNACK via Assigned Client Identifier).

Link copied to clipboard

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.

Link copied to clipboard

Default retain flag applied to convenience MqttClient.publish calls when no explicit retain value is specified. Defaults to false.

Link copied to clipboard

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.

Link copied to clipboard

Optional MqttLogger implementation that receives log messages from the library. When null (default), no logging occurs and zero overhead is incurred.

Link copied to clipboard

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).

Link copied to clipboard

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).

Link copied to clipboard

Maximum number of consecutive reconnection attempts before giving up. 0 = unlimited attempts. Defaults to 0 (unlimited).

Link copied to clipboard
val password: ByteString?

Optional password for authentication, stored as an immutable ByteString (§3.1.3.6).

Link copied to clipboard

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).

Link copied to clipboard

Initial delay between reconnection attempts in milliseconds. Doubles after each failed attempt up to reconnectMaxDelayMs. Must be > 0.

Link copied to clipboard

Maximum delay between reconnection attempts in milliseconds. Must be ≥ reconnectBaseDelayMs.

Link copied to clipboard

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).

Link copied to clipboard

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).

Link copied to clipboard

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).

Link copied to clipboard

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).

Link copied to clipboard

Optional username for simple password-based authentication (§3.1.3.5).

Link copied to clipboard

Application-defined key-value string pairs sent with the CONNECT packet. Keys may repeat. Forwarded to the broker but not to other clients.

Link copied to clipboard

Optional will message configuration. If set, the broker publishes this message when the client disconnects unexpectedly (§3.1.3.2).