| Crates.io | myrtio-mqtt |
| lib.rs | myrtio-mqtt |
| version | 0.2.0 |
| created_at | 2025-12-20 13:35:36.626301+00 |
| updated_at | 2026-01-23 18:27:24.448829+00 |
| description | MQTT client library for embedded systems |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1996516 |
| size | 124,405 |
An async, no_std, no_alloc MQTT client for embedded systems, built on the Embassy ecosystem.
no_std: Designed for bare-metal microcontrollers (ESP32, etc.) and asynchronous execution.heapless for fixed-size buffers and internal state management.embassy-net, UART, or any reliable stream-based channel via the MqttTransport trait.MqttRuntime for building applications using object-safe MqttModules.async fn in traits.Use MqttClient directly for simple, low-level control.
use myrtio_mqtt::{MqttClient, MqttOptions, MqttEvent, QoS, TcpTransport};
use embassy_time::Duration;
let transport = TcpTransport::new(socket, Duration::from_secs(5));
let options = MqttOptions::new("my-device-id");
// Client with space for 8 subscriptions and 1024-byte buffers
let mut client = MqttClient::<_, 8, 1024>::new(transport, options);
client.connect().await?;
client.subscribe("sensors/data", QoS::AtMostOnce).await?;
loop {
// Poll handles keep-alives and incoming packets
if let Ok(Some(MqttEvent::Publish(msg))) = client.poll().await {
// Handle incoming message
// msg.payload borrows from client.rx_buffer
}
}
Use MqttRuntime to compose multiple independent features (Home Assistant, Telemetry, etc.) into one event loop.
use myrtio_mqtt::runtime::{MqttModule, TopicCollector, PublishOutbox};
use myrtio_mqtt::packet::Publish;
use embassy_time::Duration;
struct TelemetryModule;
impl MqttModule for TelemetryModule {
fn register(&self, collector: &mut dyn TopicCollector) {
// Register topics for subscription
collector.add("device/commands");
}
fn on_message(&mut self, msg: &Publish<'_>) {
// Process incoming messages synchronously
}
fn on_tick(&mut self, outbox: &mut dyn PublishOutbox) -> Duration {
// Periodic tasks: publish state
outbox.publish("device/telemetry", b"{\"temp\": 22.5}", QoS::AtMostOnce);
Duration::from_secs(30)
}
}
Publish messages borrow their topic and payload directly from the client's internal receive buffer. They are only valid until the next call to poll() or until the module's on_message returns.MqttModule trait is object-safe (dyn MqttModule), allowing you to store modules in StaticCells or compose them using ModulePair without complex generic parameters.PublishOutbox. The MqttRuntime performs the actual async publishing after the module callback completes.| Module | Key Types |
|---|---|
| Root | MqttClient, MqttOptions, MqttEvent, QoS |
transport |
MqttTransport, TcpTransport |
runtime |
MqttRuntime, MqttModule, TopicCollector, PublishOutbox, PublisherHandle |