Connect with Rust
Below is an example using the paho.mqtt.rust crate with the TOKEN authentication mode configured on a Broker.
The example below creates a simple subscriber, sends a message to the configured topic, and waits until the message is received before exiting.
Make sure to set the BROKER_URI
(e.g. mqtts://YOUR-BROKER.YOUR-NAMESPACE.cloudflarepubsub.com
), BROKER_TOKEN
(a valid auth token), and BROKER_TOPIC
environmental variables before running the example program.
# in your Cargo.tomlpaho-mqtt = "0.11.1"
Create a file called example.rs
with the following content, and use cargo run
to build and run the example:
use paho_mqtt::*;use std::thread;fn main() {// Specify MQTT broker hostname: <broker name>.<namespace>.cloudflarepubsub.comlet uri = std::env::var("BROKER_URI").expect("URI must be set");// Your JWT tokenlet jwt = std::env::var("BROKER_TOKEN").expect("JWT must be set");// Specify a topic namelet topic = std::env::var("BROKER_TOPIC").expect("Topic must be set");// Configure the MQTT clientlet client_opts = CreateOptionsBuilder::new().mqtt_version(MQTT_VERSION_5).server_uri(uri).finalize();// Connect optionslet options = ConnectOptionsBuilder::new().ssl_options(SslOptions::default()).clean_start(true).password(jwt).finalize();// Create the MQTT clientlet cli = Client::new(client_opts).expect("Error creating client");// Connect to your brokercli.connect(options).expect("Error connecting to broker");// Message receiverlet rx = cli.start_consuming();// Subscribe to a topiccli.subscribe(&topic, 0).expect("Error subscribing to topic");// Start waiting for messageslet reader = thread::spawn(move || match rx.recv().expect("Error receiving message") {Some(message) => {println!("{:?}", message);}None => {}});// Publish a message!cli.publish(Message::new(topic, "My first MQTT message", 0)).expect("Error publishing");// Wait until we have received our messagelet _ = reader.join();// Good-Byecli.disconnect(DisconnectOptions::default()).expect("Error disconnecting");}