20 releases (11 breaking)

0.12.0 Mar 13, 2024
0.11.2 Feb 8, 2022
0.11.1 Oct 20, 2021
0.11.0 Mar 29, 2021
0.2.0 Jul 23, 2016

#338 in Network programming

Download history 244/week @ 2024-08-10 227/week @ 2024-08-17 187/week @ 2024-08-24 194/week @ 2024-08-31 165/week @ 2024-09-07 263/week @ 2024-09-14 504/week @ 2024-09-21 222/week @ 2024-09-28 103/week @ 2024-10-05 170/week @ 2024-10-12 121/week @ 2024-10-19 171/week @ 2024-10-26 462/week @ 2024-11-02 630/week @ 2024-11-09 336/week @ 2024-11-16 264/week @ 2024-11-23

1,710 downloads per month
Used in 9 crates (4 directly)

MIT/Apache

115KB
2.5K SLoC

MQTT-rs

Build Status Build & Test License crates.io dependency status

MQTT protocol library for Rust

[dependencies]
mqtt-protocol = "0.12"

Usage

extern crate mqtt;

use std::io::Cursor;

use mqtt::{Encodable, Decodable};
use mqtt::packet::{VariablePacket, PublishPacket, QoSWithPacketIdentifier};
use mqtt::TopicName;

fn main() {
    // Create a new Publish packet
    let packet = PublishPacket::new(TopicName::new("mqtt/learning").unwrap(),
                                    QoSWithPacketIdentifier::Level2(10),
                                    "Hello MQTT!");

    // Encode
    let mut buf = Vec::new();
    packet.encode(&mut buf).unwrap();
    println!("Encoded: {:?}", buf);

    // Decode it with known type
    let mut dec_buf = Cursor::new(&buf[..]);
    let decoded = PublishPacket::decode(&mut dec_buf).unwrap();
    println!("Decoded: {:?}", decoded);
    assert_eq!(packet, decoded);

    // Auto decode by the fixed header
    let mut dec_buf = Cursor::new(&buf[..]);
    let auto_decode = VariablePacket::decode(&mut dec_buf).unwrap();
    println!("Variable packet decode: {:?}", auto_decode);
    assert_eq!(VariablePacket::PublishPacket(packet), auto_decode);
}

Note

Dependencies

~0.3–6.5MB
~40K SLoC