mqtt5-protocol
MQTT v5.0 and v3.1.1 protocol implementation - packets, encoding, and validation.
Features
no_stdcompatible - Works on embedded systems without heap or standard library- Platform-agnostic - Runs on Linux, macOS, Windows, WASM, and bare-metal embedded targets
- Zero-copy parsing - Uses
bytescrate for efficient buffer management - Full MQTT v5.0 - All packet types, properties, and reason codes
Installation
[]
= "0.7"
For Embedded (no_std)
[]
= { = "0.6", = false }
For Single-Core Embedded
[]
= { = "0.6", = false, = ["embedded-single-core"] }
Supported Targets
| Target | Platform | Notes |
|---|---|---|
x86_64-*, aarch64-* |
Desktop/Server | Full std support |
wasm32-unknown-unknown |
WebAssembly | Browser/Node.js |
thumbv7em-none-eabihf |
ARM Cortex-M4F | STM32F4, nRF52, etc. |
riscv32imc-unknown-none-elf |
RISC-V | ESP32-C3, BL602, etc. |
Usage
Standard (std)
use ConnectPacket;
use MqttPacket;
use ConnectOptions;
use BytesMut;
let options = new;
let connect = new;
let mut buf = new;
connect.encode?;
Embedded (no_std)
extern crate alloc;
use ConnectPacket;
use MqttPacket;
use ConnectOptions;
use ;
use BytesMut;
Embedded Time Provider
On no_std targets, time functions (Instant::now(), SystemTime::now()) require external time sources since there's no OS. The library provides a time provider mechanism:
use ;
// Call from your timer interrupt or main loop
// Now Instant::now() and elapsed() work correctly
let start = now;
// ... do work ...
let elapsed = start.elapsed;
Time Provider Functions
| Function | Purpose |
|---|---|
set_time_source(monotonic, epoch) |
Initialize both time sources at startup |
update_monotonic_time(millis) |
Update monotonic clock (call periodically) |
update_epoch_time(millis) |
Update wall clock (if epoch time available) |
Implementation Notes
- Uses
AtomicU32pairs for 32-bit target compatibility - Thread-safe with
SeqCstordering - Returns 0/epoch if time source not initialized
Crate Organization
This crate provides the protocol layer used by:
- mqtt5 - Native client and broker for Linux, macOS, Windows
- mqtt5-wasm - WebAssembly client and broker for browsers
Module Structure
| Module | Description |
|---|---|
packet |
MQTT packet types (CONNECT, PUBLISH, SUBSCRIBE, etc.) |
encoding |
Binary encoding/decoding (strings, binary data, variable integers) |
protocol::v5 |
MQTT v5.0 properties and reason codes |
validation |
Topic validation, namespace rules (AWS IoT, etc.) |
session |
Session state (flow control, queues, subscriptions) - no_std compatible |
time |
Platform-agnostic time types (Duration, Instant, SystemTime) |
types |
Core types (QoS, RetainHandling, etc.) |
Features
| Feature | Description | Default |
|---|---|---|
std |
Standard library support, enables thiserror and tracing | Yes |
embedded-single-core |
Assume single-core for atomic operations | No |
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.