rust-mqtt

rust-mqtt provides an MQTT client primarily for no_std environments. The library provides an async API depending on embedded_io_async's traits. As of now, only MQTT version 5.0 is supported.
The design goal is a strict yet flexible and explicit API that leverages Rust's type system to enforce the MQTT specification while exposing all protocol features transparently. Session state, configuration, and Quality of Service message delivery and retry behaviour remain fully under user control, giving complete freedom over protocol usage. Protocol-related errors are prevented by the client API and are modeled in a way that enables maximum recoverability. By avoiding opinionated design choices and making no assumptions about the runtime environment, rust-mqtt remains lightweight while providing a powerful MQTT client foundation.
rust-mqtt does not implement opinionated connection management — automatic reconnects, keepalive loops, retry policies, or background tasks are intentionally left to the user. Instead, the crate provides cancel-safe protocol primitives, suitable for higher-level clients, tooling, and resource-constrained embedded applications. In the future, the client will be extended with additional I/O traits such as ReadReady to further composability.
Library state
Supported MQTT features
- Will
- Bidirectional publications with Quality of Service 0, 1 and 2
- Flow control
- Configuration & session tracking
- Session recovery
- Client- & serverside maximum packet size
- Subscription identifiers
- Message expiry interval
- Topic alias
- Request/Response
- Reason String in CONNACK & DISCONNECT packets
Currently unsupported MQTT features & limitations
- AUTH packet
- Properties: Authentication Method, Authentication Data, Request Problem Information, Reason String (PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK), User Property
- Subscribing to multiple topics in a single packet
Extension plans (more or less by priority)
- More versatile IO model allowing for more cancel-safety
- Sync implementation
- MQTT version 3.1.1
Feature flags
bump: Adds a simple bump allocatorBufferProviderimplementationalloc: Adds anOwned(Box<[u8]>)variant toBytesand a heap-allocation basedBufferProviderimplementation using thealloccratev3: Unusedv5: Enables MQTT version 5.0- Logging-related:
log: Enables logging via thelogcratedefmt: Implementsdefmt::Formatfor crate items & enables logging via thedefmtcrate (version 1)log-level-*: Enables logs at the selected level and more severe levels (error, warn, info, debug, trace)log-verbose: Enables high-overhead IO traces at the trace log level and enableslog-level-trace
Usage
It is recommended to use a buffering Write implementation, as the current IO model makes fragmented Write::write calls. The client also calls Read::read frequently; if your underlying implementation involves expensive syscalls, consider using a buffering reader as well.
Illustrative API example
Showing explicit session recovery and Quality of Service 2 retransmission after a network failure. The precise network and executor setup is omitted for brevity.
async
Examples
- 'demo' is a showcase of rust-mqtt's features over TCP. Note that the example usage is very strict and not really a good way of using the client.
- 'tls' connects the client to a broker over TLS with client certificate authentication and server certificate verification using embedded-tls.
Set up the broker for 'demo' by installing, configuring and running Mosquitto using the CI configuration:
Set up the broker for 'tls' by running Mosquitto with the tls config file. The required PKI files have been generated using the .ci/pki/generate.sh script.
Then you can run the examples with different logging configs and the bump/alloc features:
RUST_LOG=info
RUST_LOG=info
RUST_LOG=trace
Tests
Unit tests should be ran using both the 'alloc' and 'bump' features.
For integration tests, you can set up the mosquitto broker as used in the CI pipeline. You should restart the broker after every run of the integration test suite as it carries non-idempotent state that will impact the tests.
Then you can run integration tests with the alloc feature.
It can be helpful to see logging output when running tests.
RUST_LOG=trace
RUST_LOG=warn
RUST_LOG=info
The full test suite can run with the alloc feature, just make sure a fresh broker is up and running.
Acknowledgment
This project could not be in state in which currently is without Ulf Lilleengen and the rest of the community from Drogue IoT.
Contact
For any information, open an issue if your matter could be helpful or interesting for others or should be documented. Otherwise contact us on email julian.jg.graf@gmail.com, ond.babec@gmail.com.