#mqtt #mosquitto #client #pubsub


Rust interface to the Mosquitto MQTT broker client. This is a fork of https://github.com/jsloth/mosquitto-client

11 releases

Uses old Rust 2015

0.3.1 Jan 5, 2021
0.3.0 Jan 5, 2021
0.2.7 Jan 3, 2021
0.2.5 Dec 28, 2020
0.1.6 Dec 26, 2020

#1972 in Network programming

Download history 31/week @ 2023-10-28 14/week @ 2023-11-04 21/week @ 2023-11-11 11/week @ 2023-11-18 44/week @ 2023-11-25 37/week @ 2023-12-02 18/week @ 2023-12-09 8/week @ 2023-12-16 42/week @ 2023-12-23 5/week @ 2023-12-30 7/week @ 2024-01-06 13/week @ 2024-01-13 27/week @ 2024-01-20 27/week @ 2024-01-27 7/week @ 2024-02-03 62/week @ 2024-02-10

124 downloads per month
Used in cerk_port_mqtt_mosquitto

MIT license

792 lines

A Rust interface to the Mosquitto MQTT broker client

build Crates.io

Mosquitto is a popular MQTT broker implemented in C. Although there are pure Rust MQTT clients, it is still useful to have a binding to the Mosquitto client.

The basic story is that you connect to a broker, subscribing to topics that interest you and publishing messages on a particular topic. The messages may be any arbitrary bytes, but this implementation does require that the topics themselves be UTF-8. The C API is based on callbacks, which are mapped onto Rust closures.

The Mosquitto client is thread-safe, so you can publish from one thread and listen for the messages on another. This example demonstrates mosquitto-client usage:

extern crate mosquitto_client_wrapper as mosq;
use mosq::Mosquitto;
use std::{thread, time};

fn main() {
    let m = Mosquitto::new("test");

    m.connect("localhost",1883,5).expect("can't connect");
    m.subscribe("bilbo/#",1).expect("can't subscribe to bonzo");

    let mt = m.clone();
    thread::spawn(move || {
        let timeout = time::Duration::from_millis(500);
        for _ in 0..5 {
            mt.publish("bilbo/baggins","hello dolly".as_bytes(), 1, false).unwrap();

    let mut mc = m.callbacks(0);
    mc.on_message(|data,msg| {
        println!("bilbo {:?}",msg);
        *data += 1;

    m.loop_until_disconnect(200).expect("broken loop");
    println!("received {} messages",mc.data);

The Mosquitto struct is a thin wrapper around the C pointer we get from the client. It is Clone + Send + Sync, so we can pass it to a thread which simply publishes some bytes and waits; at the end of the thread we explicitly disconnect.

The Callbacks handler struct is separate, to avoid antagonizing the borrow checker. It is created by the callbacks method and is generic over some data (accessed as the data field) Whenever an event occurs, the callback will be passed a mutable reference to that data, and event-specific data - in this case a message struct.

loop_until_disconnect is a relative of loop_forever which ends without error if we explicitly disconnect from the broker.


bundled (on by default)

This feature will automatically download and build the mosquitto client library. By default, the master branch is taken. This could be overwritten with the following variables:

Cross compilation

The library could be cross-compiled. The following env parameters have to be set for that:

  • MOSQUITTO_CROSS_COMPILER default none, e.g. arm-linux-gnueabihf-
  • MOSQUITTO_CC default gcc

No runtime deps