
Asynchronous, buffer-backed MQTT 3.1.1 and 5.0 client with zero-copy payloads, typed codecs, automatic reconnection, QoS state machines, persistence, composable transports and high-availability failover.
Coroutines-based Kotlin Multiplatform MQTT 3.1.1 (v4) & 5.0 client. Buffer-backed, zero-copy, backed by 5000+ tests.
This project simplifies managing an MQTT client across multiple platforms with a single Kotlin API.
| Concern | Without | With com.ditchoom:mqtt-client
|
|---|---|---|
| Platform I/O | Separate MQTT stacks per platform | One coroutines API on JVM, Android, iOS/macOS/tvOS/watchOS, JS (browser + Node), Linux |
| Protocol | Pick a v3.1.1 or v5 library | Both MQTT 3.1.1 (v4) and 5.0 behind one client |
| Reliability | Hand-roll reconnect + QoS state | Automatic reconnection, QoS 0/1/2 state machines, message persistence, offline buffering |
| Payloads |
ByteArray copies everywhere |
Zero-copy ReadBuffer + typed Codec<P> payloads |
| Transport | Rewrite for TCP vs WebSocket | Inject any socket Transport: TCP, TLS, WebSocket, QUIC, WebTransport, a fallback chain, or your own |
| High availability | Manual failover | Pass an ordered list of failover endpoints and the client works through them |
@ProtocolMessage) — no hand-written wire layer — allowing customization or custom protocols derived from MQTT without a full rewrite.ByteArray copies on the hot path.Buffer uses native buffers to pass to the socket:
| Platform | Native Buffer Type |
|---|---|
| Android / JVM | ByteBuffer |
| iOS/macOS/tvOS/watchOS | NSData |
| BrowserJS/NodeJS | ArrayBuffer / SharedArrayBuffer |
| Linux x64/arm64 | NativeBuffer |
Socket uses native socket APIs:
| Platform | Native Socket Impl |
|---|---|
| Android/JVM | AsynchronousSocketChannel (fallback SocketChannel) |
| iOS/macOS/tvOS/watchOS | NWConnection |
| NodeJS | Net module |
| BrowserJS | unavailable (use WebSocket / WebTransport) |
| Linux x64/arm64 | io_uring / epoll |
Persistence uses:
| Platform | Persistence Impl |
|---|---|
| Android/JVM | SQLite via SQLDelight |
| iOS/macOS/tvOS/watchOS | SQLite via SQLDelight using -lsqlite3
|
| NodeJS | In-memory |
| BrowserJS | IndexedDB (SQLite-wasm upcoming) |
| Linux x64/arm64 | SQLite via SQLDelight using -lsqlite3
|
An in-memory Persistence is available on every platform.
DitchOoM Kotlin Multiplatform runtime dependencies:
Codec framework, and the buffer-flow byte layer.Transport interface, TCP and TLS. Exposed as an api dependency of mqtt-client. QUIC (socket-quic-default) and WebTransport (socket-webtransport) are separate, optional artifacts.websocket-tcp, with TLS and permessage-deflate compression).Official Kotlin Multiplatform runtime dependencies:
Other runtime dependencies:
| Platform | MQTT 3.1.1 (v4) | MQTT 5.0 | LWT | SSL / TLS | Persistence | Auto Reconnect | Offline Buffering | TCP | WebSocket | Coroutines API | High Availability |
|---|---|---|---|---|---|---|---|---|---|---|---|
JVM |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
Browser |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | ⛔ | ⛔ | 🚀 | 🚀 |
Node.JS |
🚀 | 🚀 | 🚀 | 🚀 | 📝 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
Android |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
iOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
macOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
watchOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
tvOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
Linux |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
🚀 = Ready 📝 = Planned ⛔ = Unavailable (platform API limits)
The WebSocket transport (websocket-tcp) runs over a TCP socket, so it is not available in the
browser. In the browser, use WebTransport or a custom
Transport.
Published to Maven Central under com.ditchoom.
dependencies {
// The client. Exposes com.ditchoom:socket (TCP + TLS) as an api dependency.
implementation("com.ditchoom:mqtt-client:3.0.0")
// The protocol version(s) you need, or both:
implementation("com.ditchoom:mqtt-4-models:3.0.0") // MQTT 3.1.1 (v4)
implementation("com.ditchoom:mqtt-5-models:3.0.0") // MQTT 5.0
// Only for the transports you use:
implementation("com.ditchoom:websocket-tcp:2.1.1") // WebSocket
implementation("com.ditchoom:socket-quic-default:4.26.0") // QUIC (experimental)
implementation("com.ditchoom:socket-webtransport:4.26.0") // WebTransport (experimental)
}Find the latest version on Maven Central.
The client is in-process: you create a Persistence, register a broker, and start an MqttClient
with an MqttEndpoint that says how to reach the broker. It connects, keeps itself connected
(automatic reconnection, plus failover across any extra endpoints), and persists in-flight QoS 1/2
messages.
import com.ditchoom.mqtt.client.MqttClient
import com.ditchoom.mqtt.client.MqttEndpoint
import com.ditchoom.mqtt.controlpacket.QualityOfService
import com.ditchoom.mqtt.controlpacket.TopicFilter
import com.ditchoom.mqtt.controlpacket.OpaquePublishPayloadCodec
import com.ditchoom.mqtt3.controlpacket.ConnectionRequest // v4; use com.ditchoom.mqtt5.* for v5
import com.ditchoom.buffer.Charset
import com.ditchoom.buffer.codec.asReadBuffer
import com.ditchoom.buffer.toReadBuffer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.take
val scope = CoroutineScope(Dispatchers.Default)
// 1. The session (CONNECT) and how to reach the broker (endpoint).
val connectionRequest = ConnectionRequest(clientId = "my-client", keepAliveSeconds = 60, cleanSession = true)
val endpoint = MqttEndpoint(host = "test.mosquitto.org", port = 1883)
// 2. Choose persistence (in-memory here) and register the broker.
val persistence = connectionRequest.controlPacketFactory.defaultPersistence(inMemory = true)
val broker = persistence.addBroker(connectionRequest)
// 3. Start the client (stays connected until you shut it down).
val client = MqttClient.start(scope = scope, broker = broker, persistence = persistence, endpoint = endpoint)
client.awaitConnectivity()
// 4. Observe messages on a topic filter. A Codec<P> decodes the payload; OpaquePublishPayloadCodec
// hands you the raw bytes. Register the observer before/at subscribe time.
val messages = client.observe(TopicFilter.fromOrThrow("test/+"), OpaquePublishPayloadCodec)
scope.launch {
messages.take(1).collect { (publish, payload) ->
val text = payload.handle.asReadBuffer().let { it.readString(it.remaining(), Charset.UTF8) }
println("Received on ${publish.topic}: $text")
}
}
// 5. Subscribe, then publish.
client.subscribe("test/+", OpaquePublishPayloadCodec, QualityOfService.AT_LEAST_ONCE).subAck.await()
client.publish(
topicName = "test/123",
qos = QualityOfService.EXACTLY_ONCE,
payload = "hello".toReadBuffer(Charset.UTF8),
)
// 6. Clean up (drain = true waits for in-flight QoS 1/2 acks first).
client.unsubscribe("test/+").unsubAck.await()
client.shutdown(sendDisconnect = true, drain = true)publish, subscribe, and observe are generic over a payload type P and take a Codec<P>:
// Untyped / raw bytes:
client.publish(topicName = "t", payload = bytes /* ReadBuffer */, qos = QualityOfService.AT_LEAST_ONCE)
// Typed — supply a Codec<P> (hand-written or KSP-generated via @ProtocolMessage):
client.publish(topic = "t", payload = myValue, payloadCodec = MyCodec, qos = QualityOfService.AT_LEAST_ONCE)
client.observe(TopicFilter.fromOrThrow("t"), MyCodec).collect { (publish, value) -> /* value: P */ }OpaquePublishPayloadCodec is the built-in codec for raw application bytes. See the
typed payloads guide for defining your own.
An MqttEndpoint carries any socket Transport. The client frames MQTT on whatever byte stream the
transport returns, so TCP, TLS, WebSocket, QUIC, WebTransport, a FallbackTransport chain and your own
Transport implementations all work the same way. TLS is set on the endpoint's TransportConfig.
import com.ditchoom.mqtt.client.MqttEndpoint
import com.ditchoom.socket.TransportConfig
import com.ditchoom.websocket.tcp.WebSocketTransport
val tcp = MqttEndpoint("broker.example.com", 1883)
val tls = MqttEndpoint("broker.example.com", 8883, config = TransportConfig.tlsDefault())
val wss =
MqttEndpoint(
host = "broker.example.com",
port = 443,
transport = WebSocketTransport(websocketEndpoint = "/mqtt", protocols = listOf("mqtt")),
config = TransportConfig.tlsDefault(),
)
val client =
MqttClient.start(scope = scope, broker = broker, persistence = persistence, endpoint = tls, failover = listOf(wss))| Transport | Class | Artifact | Status |
|---|---|---|---|
| TCP, TLS |
TcpTransport (default) |
socket (included) |
Supported |
WebSocket (ws / wss) |
com.ditchoom.websocket.tcp.WebSocketTransport |
websocket-tcp |
Supported |
| QUIC | com.ditchoom.socket.quic.QuicTransport |
socket-quic-default |
Experimental. MQTT over QUIC is non-standard but implemented by EMQX. Not available on JS. |
| WebTransport | com.ditchoom.socket.webtransport.WebTransportTransport |
socket-webtransport |
Experimental. Works in the browser. |
| Fallback chain | FallbackTransport |
socket (included) |
Supported |
See the transports guide for QUIC, WebTransport, fallback chains and custom transports.
git clone git@github.com:DitchOoM/mqtt.git# Build & test all platforms
./gradlew allTests
# Run specific platform tests
./gradlew jvmTest # JVM tests
./gradlew jsNodeTest # Node.js tests
./gradlew jsBrowserTest # Browser tests
./gradlew testDebugUnitTest # Android unit tests
./gradlew linuxX64Test # Linux native tests
./gradlew macosArm64Test # macOS tests (requires macOS)
./gradlew iosSimulatorArm64Test # iOS tests (requires macOS)
# Linting
./gradlew ktlintCheck # Check code style
./gradlew ktlintFormat # Auto-format code
# Public ABI gate (run updateKotlinAbi on macOS after an intentional API change)
./gradlew checkKotlinAbi
./gradlew updateKotlinAbi
# Wire-format snapshot gate (fails on breaking MQTT wire-format drift)
./gradlew checkCodecSchema # Verify against committed baseline
./gradlew updateCodecSchema # Accept an intentional wire change
# Publish to local maven
./gradlew publishToMavenLocal
# Get next version
./gradlew -q :mqtt-client:nextVersionSee the open issues for proposed features and known issues,
and TODO.md for tracked follow-ups.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)Distributed under the MIT License. See LICENSE for more information.
Coroutines-based Kotlin Multiplatform MQTT 3.1.1 (v4) & 5.0 client. Buffer-backed, zero-copy, backed by 5000+ tests.
This project simplifies managing an MQTT client across multiple platforms with a single Kotlin API.
| Concern | Without | With com.ditchoom:mqtt-client
|
|---|---|---|
| Platform I/O | Separate MQTT stacks per platform | One coroutines API on JVM, Android, iOS/macOS/tvOS/watchOS, JS (browser + Node), Linux |
| Protocol | Pick a v3.1.1 or v5 library | Both MQTT 3.1.1 (v4) and 5.0 behind one client |
| Reliability | Hand-roll reconnect + QoS state | Automatic reconnection, QoS 0/1/2 state machines, message persistence, offline buffering |
| Payloads |
ByteArray copies everywhere |
Zero-copy ReadBuffer + typed Codec<P> payloads |
| Transport | Rewrite for TCP vs WebSocket | Inject any socket Transport: TCP, TLS, WebSocket, QUIC, WebTransport, a fallback chain, or your own |
| High availability | Manual failover | Pass an ordered list of failover endpoints and the client works through them |
@ProtocolMessage) — no hand-written wire layer — allowing customization or custom protocols derived from MQTT without a full rewrite.ByteArray copies on the hot path.Buffer uses native buffers to pass to the socket:
| Platform | Native Buffer Type |
|---|---|
| Android / JVM | ByteBuffer |
| iOS/macOS/tvOS/watchOS | NSData |
| BrowserJS/NodeJS | ArrayBuffer / SharedArrayBuffer |
| Linux x64/arm64 | NativeBuffer |
Socket uses native socket APIs:
| Platform | Native Socket Impl |
|---|---|
| Android/JVM | AsynchronousSocketChannel (fallback SocketChannel) |
| iOS/macOS/tvOS/watchOS | NWConnection |
| NodeJS | Net module |
| BrowserJS | unavailable (use WebSocket / WebTransport) |
| Linux x64/arm64 | io_uring / epoll |
Persistence uses:
| Platform | Persistence Impl |
|---|---|
| Android/JVM | SQLite via SQLDelight |
| iOS/macOS/tvOS/watchOS | SQLite via SQLDelight using -lsqlite3
|
| NodeJS | In-memory |
| BrowserJS | IndexedDB (SQLite-wasm upcoming) |
| Linux x64/arm64 | SQLite via SQLDelight using -lsqlite3
|
An in-memory Persistence is available on every platform.
DitchOoM Kotlin Multiplatform runtime dependencies:
Codec framework, and the buffer-flow byte layer.Transport interface, TCP and TLS. Exposed as an api dependency of mqtt-client. QUIC (socket-quic-default) and WebTransport (socket-webtransport) are separate, optional artifacts.websocket-tcp, with TLS and permessage-deflate compression).Official Kotlin Multiplatform runtime dependencies:
Other runtime dependencies:
| Platform | MQTT 3.1.1 (v4) | MQTT 5.0 | LWT | SSL / TLS | Persistence | Auto Reconnect | Offline Buffering | TCP | WebSocket | Coroutines API | High Availability |
|---|---|---|---|---|---|---|---|---|---|---|---|
JVM |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
Browser |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | ⛔ | ⛔ | 🚀 | 🚀 |
Node.JS |
🚀 | 🚀 | 🚀 | 🚀 | 📝 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
Android |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
iOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
macOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
watchOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
tvOS |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
Linux |
🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
🚀 = Ready 📝 = Planned ⛔ = Unavailable (platform API limits)
The WebSocket transport (websocket-tcp) runs over a TCP socket, so it is not available in the
browser. In the browser, use WebTransport or a custom
Transport.
Published to Maven Central under com.ditchoom.
dependencies {
// The client. Exposes com.ditchoom:socket (TCP + TLS) as an api dependency.
implementation("com.ditchoom:mqtt-client:3.0.0")
// The protocol version(s) you need, or both:
implementation("com.ditchoom:mqtt-4-models:3.0.0") // MQTT 3.1.1 (v4)
implementation("com.ditchoom:mqtt-5-models:3.0.0") // MQTT 5.0
// Only for the transports you use:
implementation("com.ditchoom:websocket-tcp:2.1.1") // WebSocket
implementation("com.ditchoom:socket-quic-default:4.26.0") // QUIC (experimental)
implementation("com.ditchoom:socket-webtransport:4.26.0") // WebTransport (experimental)
}Find the latest version on Maven Central.
The client is in-process: you create a Persistence, register a broker, and start an MqttClient
with an MqttEndpoint that says how to reach the broker. It connects, keeps itself connected
(automatic reconnection, plus failover across any extra endpoints), and persists in-flight QoS 1/2
messages.
import com.ditchoom.mqtt.client.MqttClient
import com.ditchoom.mqtt.client.MqttEndpoint
import com.ditchoom.mqtt.controlpacket.QualityOfService
import com.ditchoom.mqtt.controlpacket.TopicFilter
import com.ditchoom.mqtt.controlpacket.OpaquePublishPayloadCodec
import com.ditchoom.mqtt3.controlpacket.ConnectionRequest // v4; use com.ditchoom.mqtt5.* for v5
import com.ditchoom.buffer.Charset
import com.ditchoom.buffer.codec.asReadBuffer
import com.ditchoom.buffer.toReadBuffer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.take
val scope = CoroutineScope(Dispatchers.Default)
// 1. The session (CONNECT) and how to reach the broker (endpoint).
val connectionRequest = ConnectionRequest(clientId = "my-client", keepAliveSeconds = 60, cleanSession = true)
val endpoint = MqttEndpoint(host = "test.mosquitto.org", port = 1883)
// 2. Choose persistence (in-memory here) and register the broker.
val persistence = connectionRequest.controlPacketFactory.defaultPersistence(inMemory = true)
val broker = persistence.addBroker(connectionRequest)
// 3. Start the client (stays connected until you shut it down).
val client = MqttClient.start(scope = scope, broker = broker, persistence = persistence, endpoint = endpoint)
client.awaitConnectivity()
// 4. Observe messages on a topic filter. A Codec<P> decodes the payload; OpaquePublishPayloadCodec
// hands you the raw bytes. Register the observer before/at subscribe time.
val messages = client.observe(TopicFilter.fromOrThrow("test/+"), OpaquePublishPayloadCodec)
scope.launch {
messages.take(1).collect { (publish, payload) ->
val text = payload.handle.asReadBuffer().let { it.readString(it.remaining(), Charset.UTF8) }
println("Received on ${publish.topic}: $text")
}
}
// 5. Subscribe, then publish.
client.subscribe("test/+", OpaquePublishPayloadCodec, QualityOfService.AT_LEAST_ONCE).subAck.await()
client.publish(
topicName = "test/123",
qos = QualityOfService.EXACTLY_ONCE,
payload = "hello".toReadBuffer(Charset.UTF8),
)
// 6. Clean up (drain = true waits for in-flight QoS 1/2 acks first).
client.unsubscribe("test/+").unsubAck.await()
client.shutdown(sendDisconnect = true, drain = true)publish, subscribe, and observe are generic over a payload type P and take a Codec<P>:
// Untyped / raw bytes:
client.publish(topicName = "t", payload = bytes /* ReadBuffer */, qos = QualityOfService.AT_LEAST_ONCE)
// Typed — supply a Codec<P> (hand-written or KSP-generated via @ProtocolMessage):
client.publish(topic = "t", payload = myValue, payloadCodec = MyCodec, qos = QualityOfService.AT_LEAST_ONCE)
client.observe(TopicFilter.fromOrThrow("t"), MyCodec).collect { (publish, value) -> /* value: P */ }OpaquePublishPayloadCodec is the built-in codec for raw application bytes. See the
typed payloads guide for defining your own.
An MqttEndpoint carries any socket Transport. The client frames MQTT on whatever byte stream the
transport returns, so TCP, TLS, WebSocket, QUIC, WebTransport, a FallbackTransport chain and your own
Transport implementations all work the same way. TLS is set on the endpoint's TransportConfig.
import com.ditchoom.mqtt.client.MqttEndpoint
import com.ditchoom.socket.TransportConfig
import com.ditchoom.websocket.tcp.WebSocketTransport
val tcp = MqttEndpoint("broker.example.com", 1883)
val tls = MqttEndpoint("broker.example.com", 8883, config = TransportConfig.tlsDefault())
val wss =
MqttEndpoint(
host = "broker.example.com",
port = 443,
transport = WebSocketTransport(websocketEndpoint = "/mqtt", protocols = listOf("mqtt")),
config = TransportConfig.tlsDefault(),
)
val client =
MqttClient.start(scope = scope, broker = broker, persistence = persistence, endpoint = tls, failover = listOf(wss))| Transport | Class | Artifact | Status |
|---|---|---|---|
| TCP, TLS |
TcpTransport (default) |
socket (included) |
Supported |
WebSocket (ws / wss) |
com.ditchoom.websocket.tcp.WebSocketTransport |
websocket-tcp |
Supported |
| QUIC | com.ditchoom.socket.quic.QuicTransport |
socket-quic-default |
Experimental. MQTT over QUIC is non-standard but implemented by EMQX. Not available on JS. |
| WebTransport | com.ditchoom.socket.webtransport.WebTransportTransport |
socket-webtransport |
Experimental. Works in the browser. |
| Fallback chain | FallbackTransport |
socket (included) |
Supported |
See the transports guide for QUIC, WebTransport, fallback chains and custom transports.
git clone git@github.com:DitchOoM/mqtt.git# Build & test all platforms
./gradlew allTests
# Run specific platform tests
./gradlew jvmTest # JVM tests
./gradlew jsNodeTest # Node.js tests
./gradlew jsBrowserTest # Browser tests
./gradlew testDebugUnitTest # Android unit tests
./gradlew linuxX64Test # Linux native tests
./gradlew macosArm64Test # macOS tests (requires macOS)
./gradlew iosSimulatorArm64Test # iOS tests (requires macOS)
# Linting
./gradlew ktlintCheck # Check code style
./gradlew ktlintFormat # Auto-format code
# Public ABI gate (run updateKotlinAbi on macOS after an intentional API change)
./gradlew checkKotlinAbi
./gradlew updateKotlinAbi
# Wire-format snapshot gate (fails on breaking MQTT wire-format drift)
./gradlew checkCodecSchema # Verify against committed baseline
./gradlew updateCodecSchema # Accept an intentional wire change
# Publish to local maven
./gradlew publishToMavenLocal
# Get next version
./gradlew -q :mqtt-client:nextVersionSee the open issues for proposed features and known issues,
and TODO.md for tracked follow-ups.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)Distributed under the MIT License. See LICENSE for more information.