Skip to content

Commit c45a672

Browse files
committed
update MQTT tokio demo to include TLS transport
1 parent cebe402 commit c45a672

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ target
2121
#.idea/
2222

2323
# Environment files
24-
.env
24+
.env
25+
26+
# Dev files
27+
dev

examples/tokio-mqtt-connector-demo/src/main.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@
3333
//! ```bash
3434
//! mosquitto_pub -h localhost -t 'commands/temp/indoor' -m '{"action":"read","sensor_id":"indoor-001"}'
3535
//! ```
36+
//!
37+
//! ## TLS (mqtts://)
38+
//!
39+
//! The broker URL is read from `MQTT_BROKER_URL` (defaults to
40+
//! `mqtt://localhost:1883`). To exercise the `mqtts://` TLS transport against
41+
//! a local mosquitto broker configured with a self-signed CA:
42+
//!
43+
//! ```bash
44+
//! SSL_CERT_FILE=/path/to/ca.crt \
45+
//! MQTT_BROKER_URL=mqtts://localhost:8883 \
46+
//! cargo run -p tokio-mqtt-connector-demo --features tokio-runtime
47+
//! ```
48+
//!
49+
//! `SSL_CERT_FILE` is honored by the native-tls/OpenSSL backend on Linux to
50+
//! extend the trusted roots without touching the system store — required
51+
//! because a self-signed broker cert won't otherwise validate.
3652
3753
use aimdb_core::buffer::BufferCfg;
3854
use aimdb_core::{AimDbBuilder, DbResult, Producer, RecordKey, RuntimeContext};
@@ -130,12 +146,16 @@ async fn main() -> DbResult<()> {
130146

131147
let runtime = Arc::new(TokioAdapter::new()?);
132148

149+
let broker_url = std::env::var("MQTT_BROKER_URL")
150+
.unwrap_or_else(|_| "mqtt://localhost:1883".to_string());
151+
133152
println!("MQTT Connector Demo");
134153
println!("===================");
154+
println!("Broker: {broker_url}");
135155
println!();
136156

137157
let mut builder = AimDbBuilder::new().runtime(runtime).with_connector(
138-
aimdb_mqtt_connector::MqttConnector::new("mqtt://localhost:1883")
158+
aimdb_mqtt_connector::MqttConnector::new(broker_url)
139159
.with_client_id("tokio-demo-multi-sensor"),
140160
);
141161

0 commit comments

Comments
 (0)