17 releases (10 breaking)

new 0.11.2 May 8, 2024
0.11.1 Jan 14, 2024
0.10.0 Jan 6, 2024
0.7.0 Dec 27, 2023
0.4.0 Jan 3, 2021

#1552 in Network programming

Download history 3/week @ 2024-01-22 30/week @ 2024-01-29 19/week @ 2024-02-05 181/week @ 2024-02-12 52/week @ 2024-02-19 87/week @ 2024-02-26 44/week @ 2024-03-04 32/week @ 2024-03-11 26/week @ 2024-03-18 16/week @ 2024-03-25 90/week @ 2024-04-01 5/week @ 2024-04-08 1/week @ 2024-04-15 22/week @ 2024-04-22 48/week @ 2024-04-29 142/week @ 2024-05-06

213 downloads per month

MIT license

2.5MB
64K SLoC

C 45K SLoC // 0.1% comments Python 15K SLoC // 0.2% comments Rust 2.5K SLoC // 0.0% comments C++ 1.5K SLoC // 0.0% comments Shell 193 SLoC // 0.3% comments Bitbake 81 SLoC // 0.9% comments Perl 69 SLoC // 0.3% comments XSL 53 SLoC // 0.2% comments

An async MQTT client

build Crates.io

This crate implements an async MQTT client using libmosquitto.

use mosquitto_rs::*;

fn main() -> Result<(), Error> {
    smol::block_on(async {
        let mut mosq = Client::with_auto_id()?;
        let rc = mosq.connect("localhost", 1883, std::time::Duration::from_secs(5), None).await?;
        println!("connect: {}", rc);

        let subscriptions = mosq.subscriber().unwrap();

        mosq.subscribe("test", QoS::AtMostOnce).await?;
        println!("subscribed");

        mosq.publish("test", b"woot", QoS::AtMostOnce, false)
            .await?;
        println!("published");

        if let Ok(msg) = subscriptions.recv().await {
            println!("msg: {:?}", msg);
        }

        Ok(())
    })
}

lib.rs:

This crate implements an async MQTT client using libmosquitto.

use mosquitto_rs::*;

fn main() -> Result<(), Error> {
    smol::block_on(async {
        let mut client = Client::with_auto_id()?;
        let rc = client.connect(
                       "localhost", 1883,
                       std::time::Duration::from_secs(5), None).await?;
        println!("connect: {}", rc);

        let subscriptions = client.subscriber().unwrap();

        client.subscribe("test", QoS::AtMostOnce).await?;
        println!("subscribed");

        client.publish("test", b"woot", QoS::AtMostOnce, false)
            .await?;
        println!("published");

        if let Ok(msg) = subscriptions.recv().await {
            println!("msg: {:?}", msg);
        }

        Ok(())
    })
}

Features

The following feature flags are available:

  • router - include the router module and MqttRouter type. This is on by default.
  • vendored-mosquitto - use bundled libmosquitto 2.4 library. This is on by default.
  • vendored-mosquitto-tls - enable tls support in the bundled libmosquitto. This is on by default.
  • vendored-openssl - build openssl from source, rather than using the system library. Recommended for macOS and Windows users to enable this.

Dependencies