MqttMessage

data class MqttMessage(val topic: String, val payload: ByteString, val qos: QoS = QoS.AT_MOST_ONCE, val retain: Boolean = false, val properties: PublishProperties = PublishProperties())

Represents a received or outgoing MQTT message with full MQTT 5.0 properties.

This is the primary data type flowing through MqttClient.messages and passed to MqttClient.publish. Uses ByteString for the payload, guaranteeing immutability at the type level — payloads can be safely shared across coroutines.

Example

// Publish a message
client.publish(MqttMessage(
topic = "sensors/temperature",
payload = ByteString("22.5".encodeToByteArray()),
qos = QoS.AT_LEAST_ONCE,
))

// Receive messages
client.messages.collect { msg ->
println("${msg.topic}: ${msg.payload}")
}

Constructors

Link copied to clipboard
constructor(topic: String, payload: ByteString, qos: QoS = QoS.AT_MOST_ONCE, retain: Boolean = false, properties: PublishProperties = PublishProperties())
constructor(topic: String, payload: ByteArray, qos: QoS = QoS.AT_MOST_ONCE, retain: Boolean = false, properties: PublishProperties = PublishProperties())

Convenience constructor accepting raw bytes.

Properties

Link copied to clipboard
val payload: ByteString

The message payload as an immutable ByteString. May be empty.

Link copied to clipboard

MQTT 5.0 publish properties such as message expiry, content type, response topic for request/response, and user properties.

Link copied to clipboard
val qos: QoS

The Quality of Service level controlling delivery guarantees.

Link copied to clipboard

If true, the broker stores this message and delivers it to future subscribers as the "last known good" value for this topic.

Link copied to clipboard

The MQTT topic this message was published to. Must not be empty.

Functions

Link copied to clipboard

Returns the payload decoded as a UTF-8 string.

Link copied to clipboard
open override fun toString(): String