| Crates.io | mqute-codec |
| lib.rs | mqute-codec |
| version | 0.4.0 |
| created_at | 2025-07-30 22:03:01.571641+00 |
| updated_at | 2025-08-26 21:39:01.713194+00 |
| description | A full-featured implementation of MQTT protocol serialization in Rust, supporting versions 3.1, 3.1.1 and 5.0. |
| homepage | |
| repository | https://github.com/evgeniygem/mqute-codec |
| max_upload_size | |
| id | 1774121 |
| size | 328,429 |
A feature-complete, zero-allocation implementation of MQTT (Message Queuing Telemetry Transport) protocol serialization in Rust, supporting versions 3.1, 3.1.1, and 5.0 with strict protocol compliance and validation.
mqute-codec is a high-performance MQTT packet serialization/deserialization library designed for building robust MQTT clients, brokers, and protocol tools. It provides:
This library focuses exclusively on the packet layer, making it ideal for:
Add to your Cargo.toml:
[dependencies]
mqute-codec = "0.4"
use mqute_codec::protocol::{Credentials, QoS};
use mqute_codec::protocol::v5::{Connect, Packet, Will};
use std::time::Duration;
use bytes::{Bytes, BytesMut};
use mqute_codec::codec::{PacketCodec, Encode, Decode};
use tokio_util::codec::{Decoder, Encoder};
fn main() {
let credentials = Credentials::full("user", "password");
let original = Packet::Connect(Connect::new(
"client",
Some(credentials),
Some(Will::new(
None,
"device/status",
Bytes::from("disconnected"),
QoS::ExactlyOnce,
true
)),
Duration::from_secs(30),
true
));
let mut codec = PacketCodec::new(Some(4096), Some(4096));
let mut buf = BytesMut::new();
original.encode(&mut buf).unwrap();
let raw = codec.try_decode(&mut buf).unwrap();
let restored = Packet::decode(raw).unwrap();
assert_eq!(original, restored);
}
use mqute_codec::protocol::v5::Packet;
use mqute_codec::codec::{PacketCodec, RawPacket};
use tokio::net::TcpListener;
use tokio_util::codec::Framed;
use futures::StreamExt;
async fn on_recv(raw: RawPacket) {
if let Ok(packet) = Packet::decode(raw) {
match packet {
Packet::Connect(packet) => unimplemented!(),
Packet::ConnAck(packet) => unimplemented!(),
Packet::Publish(packet) => unimplemented!(),
Packet::PubAck(packet) => unimplemented!(),
Packet::PubRec(packet) => unimplemented!(),
Packet::PubRel(packet) => unimplemented!(),
Packet::PubComp(packet) => unimplemented!(),
Packet::Subscribe(packet) => unimplemented!(),
Packet::SubAck(packet) => unimplemented!(),
Packet::Unsubscribe(packet) => unimplemented!(),
Packet::UnsubAck(packet) => unimplemented!(),
Packet::PingReq(packet) => unimplemented!(),
Packet::PingResp(packet) => unimplemented!(),
Packet::Disconnect(packet) => unimplemented!(),
Packet::Auth(packet) => unimplemented!(),
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:1883").await?;
loop {
let (socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut framed = Framed::new(socket, PacketCodec::new(Some(4096), None));
while let Some(frame) = framed.next().await {
match frame {
Ok(raw) => {
on_recv(raw).await;
}
Err(e) => {
eprintln!("Error processing frame: {}", e);
break;
}
}
}
});
}
}
Complete API documentation is available on docs.rs.
This project is licensed under the MIT License - see the LICENSE file for details.