GitXplorerGitXplorer
w

mosquitto-rs

public
11 stars
7 forks
4 issues

Commits

List of commits on branch main.
Verified
07d11c95d1eb22957c6e5cc58bdfac5ff84787b7

Prep for release

wwez committed 8 months ago
Verified
b5164016e62ca6ae36bb4c047f01dec22a817092

fixup for from_raw_parts precondition check on rust 1.78

wwez committed 8 months ago
Verified
c833dcb1d0bf70d9e3e4d67971a87523110c6970

Cancel all mids if we get disconnected

wwez committed a year ago
Verified
152eb09c5143ad772805298afde759d05c5f1c1f

router: don't require futures to be Sync

wwez committed a year ago
Verified
0086f4f00464f860732d003adcd6a621b854d783

API changse to improve handling of disconnects, and logging

wwez committed a year ago
Verified
3ee72b3bdd703150f8c91d3d6f51265f1750c125

libmosquitto-sys: expose logging constants

wwez committed a year ago

README

The README file for this repository.

Mosquitto MQTT client in Rust

build Crates.io

This crate implements an async MQTT client using libmosquitto.

//! This example shows how to make a client, subscribe to a wildcard topic (`test/#`)
//! and publish a message to a topic.
//! It then waits to receive a message from the subscription (which will likely
//! be the message it just sent) and then terminates.
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/this", b"woot", QoS::AtMostOnce, false)
            .await?;
        println!("published");

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

        Ok(())
    })
}

Why?

There are already a couple of other mosquitto-based Rust crates, so why add another one? The others are various combinations of unmaintained, outdated, have code paths that can lead to panics, or that have some slightly sketchy unsafe code.

In addition, none of them offered an async interface.

Why mosquitto-based rather than a native Rust client? There are certainly a large number of native Rust clients on crates.io, but I was a bit disappointed because they all used different versions of tokio, none of them current, and the one I liked the look of the most didn't compile. To make matters more frustrating, rustls is widely used for these clients, along with webpki, but that doesn't mesh well with self-signed certificates that are prevalent among home automation environments where I'm interested in using MQTT.

So, when I realized that I'd need to spend a few hours on this, I opted for something that I could easily manage and keep ticking over without wrestling with TLS and tokio versions.

Features

The following feature flags are available:

  • vendored-mosquitto - use bundled libmosquitto 2.4 library. 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.

Windows

On Windows, you'll need to build with --feature vendored-openssl. Currently, due to https://github.com/alexcrichton/openssl-src-rs/issues/82, you'll need to deploy the dlls found in a randomized directory such as target\release\build\openssl-sys-HASH\out\openssl-build\install\bin alongside your application for it to start up correctly.