Skip to main content

MQTT Rust Client

This Rust client provides an interface which is intended to mirror the Paho Java and C++ API's as closely as possible. It requires the Paho MQTT C client library.

Note that the Rust client is still in early, pre-release development, and is due for a formal release in early 2018.

Features

Source

Source is available from the GitHub repository.

Download

The project is availble on the Rust crates.io site as paho-mqtt. Include it in a client application simply by adding a depency to the project's Cargo.toml file, like:

[dependencies]
paho-mqtt = "0.5"

Building from source

The project uses the standard Rust project/package manager, Cargo. Simply clone the repository and run cargo build

See the GitHub page for additional requirements and build instructions.

Documentation

Reference documentation is on Rust DOCS.RS, and also here on eclipse.org.

Getting Started

There are a number of small sample applications in the examples directory of the repository. These can all be built with the command:

cargo build --examples

Here is a simple example of publishing with the Rust asynchronous API:

extern crate paho_mqtt as mqtt;

use std::process;

fn main() {
    // Create a client & define connect options
    let cli = mqtt::AsyncClient::new("tcp://localhost:1883").unwrap_or_else(|err| {
        println!("Error creating the client: {}", err);
        process::exit(1);
    });

    let conn_opts = mqtt::ConnectOptions::new();

    // Connect and wait for it to complete or fail
    if let Err(e) = cli.connect(conn_opts).wait() {
        println!("Unable to connect: {:?}", e);
        process::exit(1);
    }

    // Create a message and publish it
    println!("Publishing a message on the 'test' topic");
    let msg = mqtt::Message::new("test", "Hello world!", 0);
    let tok = cli.publish(msg);

    if let Err(e) = tok.wait() {
        println!("Error sending message: {:?}", e);
    }

    // Disconnect from the broker
    let tok = cli.disconnect(None);
    tok.wait().unwrap();
}

Back to the top