forked from AtherEnergy/rumqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub1.rs
32 lines (27 loc) · 1.2 KB
/
pubsub1.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use rumqtt::{MqttClient, MqttOptions, QoS, ReconnectOptions};
use std::{thread, time::Duration};
fn main() {
pretty_env_logger::init();
//let broker = "prod-mqtt-broker.atherengineering.in";
let broker = "test.mosquitto.org";
let port = 1883;
let reconnection_options = ReconnectOptions::Always(10);
let mqtt_options = MqttOptions::new("test-pubsub2", broker, port)
.set_keep_alive(10)
.set_inflight(3)
.set_request_channel_capacity(3)
.set_reconnect_opts(reconnection_options)
.set_clean_session(false);
let (mut mqtt_client, notifications) = MqttClient::start(mqtt_options).unwrap();
mqtt_client.subscribe("hello/world", QoS::AtLeastOnce).unwrap();
thread::spawn(move || {
for i in 0..100 {
let payload = format!("publish {}", i);
thread::sleep(Duration::from_millis(1000));
mqtt_client.publish("hello/world", QoS::AtLeastOnce, false, payload).unwrap();
}
});
for notification in notifications {
println!("{:?}", notification)
}
}