7 unstable releases (3 breaking)

0.4.0 Dec 1, 2020
0.3.0 Mar 23, 2020
0.2.0 Oct 25, 2019
0.1.4 Sep 16, 2019
0.1.0 Feb 24, 2019

#95 in Network programming

Download history 57/week @ 2020-12-29 78/week @ 2021-01-05 72/week @ 2021-01-12 126/week @ 2021-01-19 151/week @ 2021-01-26 58/week @ 2021-02-02 106/week @ 2021-02-09 272/week @ 2021-02-16 102/week @ 2021-02-23 182/week @ 2021-03-02 131/week @ 2021-03-09 88/week @ 2021-03-16 167/week @ 2021-03-23 59/week @ 2021-03-30 89/week @ 2021-04-06 177/week @ 2021-04-13

560 downloads per month
Used in 2 crates

Apache-2.0

80KB
1.5K SLoC

Rust Mqtt Encoding & Decoding Crates.io Docs.rs

Mqttrs is a Rust crate (library) to write MQTT protocol clients and servers.

It is a codec-only library with very few dependencies and a straightforward and composable API, usable with rust's standard library or with async frameworks like tokio. It is strict when decoding (e.g. returns an error when encountering reserved values) and encoding (the API makes it impossible to generate an illegal packet).

Mqttrs currently requires Rust >= 1.39 and supports MQTT 3.1.1. Support for MQTT 5 is planned for a future version.

Usage

Add mqttrs = "0.3" and bytes = "0.5" to your Cargo.toml.

use mqttrs::*;
use bytes::BytesMut;

// Allocate write buffer.
let mut buf = BytesMut::with_capacity(1024);

// Encode an MQTT Connect packet.
let pkt = Packet::Connect(Connect { protocol: Protocol::MQTT311,
                                    keep_alive: 30,
                                    client_id: "doc_client".into(),
                                    clean_session: true,
                                    last_will: None,
                                    username: None,
                                    password: None });
assert!(encode(&pkt, &mut buf).is_ok());
assert_eq!(&buf[14..], "doc_client".as_bytes());
let mut encoded = buf.clone();

// Decode one packet. The buffer will advance to the next packet.
assert_eq!(Ok(Some(pkt)), decode(&mut buf));

// Example decode failures.
let mut incomplete = encoded.split_to(10);
assert_eq!(Ok(None), decode(&mut incomplete));
let mut garbage = BytesMut::from(&[0u8,0,0,0] as &[u8]);
assert_eq!(Err(Error::InvalidHeader), decode(&mut garbage));

Optional serde support.

Use mqttrs = { version = "0.3", features = [ "derive" ] } in your Cargo.toml.

Enabling this features adds #[derive(Deserialize, Serialize)] to some mqttrs types. This simplifies storing those structs in a database or file, typically to implement session support (qos, subscriptions...).

This doesn't add mqtt as a serde data format; you still need to use the mqttrs::{decode,encode} functions.

Optional #[no_std] support.

Use mqttrs = { version = "0.3", default-features = false } in your Cargo.toml to remove the default std feature.

Disabling this feature comes with the cost of not implementing the std::error::Error trait, as well as not supporting std::io read and write. This allows usage in embedded devices where the standard library is not available.

Dependencies

~0.6–0.8MB
~19K SLoC