MKTT
MQTT 5 for Kotlin Multiplatform, split into:
mktt-core: protocol types, properties, packets, codecs
mktt-client: coroutine and Flow based client built on top of mktt-core
Dependencies
dependencies {
implementation("io.github.nicolasfara:mktt-core:<version>")
implementation("io.github.nicolasfara:mktt-client:<version>")
}
Example
import io.github.nicolasfara.mktt.client.MqttClient
import io.github.nicolasfara.mktt.client.PublishRequest
io.github.nicolasfara.mktt.core.QoS
io.github.nicolasfara.mktt.core.Topic
io.github.nicolasfara.mktt.core.TopicFilter
kotlinx.coroutines.flow.first
{
client = MqttClient(, ) {
clientId =
}
client.connect()
client.subscribe(listOf(TopicFilter(Topic())))
client.publish(
PublishRequest() {
desiredQoS = QoS.AT_LEAST_ONCE
payload()
},
)
message = client.messages(TopicFilter(Topic())).first()
println(message.payloadAsString())
client.disconnect()
client.close()
}
Modules
mktt-core exposes:
- MQTT 5 reason codes and properties
- topic and subscription models
- packet definitions for CONNECT, CONNACK, PUBLISH, PUBACK/PUBREC/PUBREL/PUBCOMP, SUBSCRIBE, UNSUBSCRIBE, PING and DISCONNECT
- packet encode/decode utilities over Ktor channels and
kotlinx-io
mktt-client exposes:
MqttClient
StateFlow<MqttConnectionState>
SharedFlow<MqttPublishMessage>
Testing
mktt-core is covered by deterministic packet/property round-trip tests.
mktt-client keeps fast unit tests in commonTest/jvmTest and runs broker-based checks in jvmIntegrationTest.
Run unit tests:
./gradlew unitTest
Run integration tests against the local Mosquitto container scaffold (mktt-client/src/jvmIntegrationTest/resources):
MKTT_RUN_INTEGRATION_TESTS=true ./gradlew integrationTest
Optional public broker test (opt-in only):
MKTT_RUN_INTEGRATION_TESTS=true MKTT_RUN_REMOTE_BROKER_TESTS=true ./gradlew integrationTest