MQTT Client in Rust π¦
A lightweight and high-performance MQTT client built in Rust, demonstrating synchronous and asynchronous message publishing and subscribing using the rumqttc crate.
This project serves as a practical exploration of IoT messaging and broker communication patterns, showing how Rustβs concurrency model can handle scalable IoT data pipelines.
π Features
β Synchronous MQTT Client β Blocking publish/subscribe loop using native threads
β‘ Asynchronous MQTT Client β Fully async with tokio runtime
π¬ Last Will and Testament support
π QoS Levels (AtMostOnce, AtLeastOnce, ExactlyOnce)
π Keep Alive and heartbeat handling
π§© Clean architecture for both client and broker integration
ποΈ Architecture Overview
The project uses rumqttc β a modern Rust MQTT client library β and demonstrates how to:
Initialize MqttOptions
Configure LastWill
Spawn publish and subscribe threads (sync)
Use async task spawning with tokio
Manage event loops for message notifications
π¦ Dependencies [dependencies] rumqttc = "0.24.0" pretty_env_logger = "0.4" tokio = { version = "1", features = ["full"] }
π§© Project Structure βββ Cargo.toml βββ src β βββ syncpubsub.rs # Synchronous publish/subscribe implementation β βββ asyncpubsub.rs # Asynchronous publish/subscribe implementation
π§ Usage 1οΈβ£ Run the synchronous example cargo run --bin syncpubsub
2οΈβ£ Run the asynchronous example cargo run --bin asyncpubsub
π Default Broker
The examples use a public MQTT broker:
broker.emqx.io port: 1883
You can change this by editing the MqttOptions initialization:
let mut mqttoptions = MqttOptions::new("client-id", "your-broker-address", 1883);
π§ͺ Example Output
- Notification = Incoming(Publish(hello/1/world, payload length: 1))
- Notification = Incoming(Publish(hello/2/world, payload length: 2)) ... Done with the stream!!
π§± Learning Goals
This project helped explore:
How MQTT protocol ensures reliable IoT message delivery
The role of QoS levels in controlling network reliability
Rustβs async runtime (tokio) in managing concurrent broker communication
Comparing threaded sync vs async task performance in real-world MQTT pipelines
π Next Steps
Add TLS (MQTTS) connection support
Implement a configurable CLI for topic management
Add logging to a file/database for real IoT telemetry simulation
Benchmark latency and throughput between sync and async versions
βοΈ License MIT License Β© 2025