#mqtt #connection #pub-sub #publish #manager #manage #rumqttc

mqtt-manager

A library to manage MQTT connections in Rust. Based on rumqttc.

2 stable releases

1.0.1 Apr 23, 2024

#59 in #publish

MIT/Apache

10KB
97 lines

mqtt-manager - A library to manage MQTT connections in Rust. Based on rumqttc.

Crates.io Docs.rs MIT licensed

Changelog

1.0.0 - Initial release


lib.rs:

Convenience class for managing the MQTT connection

Examples

use mqtt_manager::*;
use std::time::Duration;

async fn handle_msg_a(pubdata: Publish) {
    println!("Received msg A: {:?}", pubdata.payload);
}

async fn handle_msg_b(pubdata: Publish) {
    println!("Received msg A: {:?}", pubdata.payload);
}

#[tokio::main]
async fn main() {
    let mut mgr = MqttManager::new("mqtt://localhost:1883/override_client_id");
    mgr.subscribe("msg/a", 0, make_callback!(handle_msg_a)).await;
    mgr.subscribe("msg/b", 0, make_callback!(handle_msg_b)).await;
    mgr.publish("msg/a", "test", 0).await;
    loop {
        tokio::select! {
            _ = mgr.process() => (),
            _ = tokio::signal::ctrl_c() => {
                mgr.disconnect().await;
                break;
            }
        }
    }
}

Dependencies

~6–15MB
~185K SLoC