Skip to content

Commit 443d486

Browse files
committed
Rustdoc examples for MQTT
Signed-off-by: itowlson <[email protected]>
1 parent 29adff9 commit 443d486

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ extern "C" fn __spin_sdk_hash() {}
5757
/// Helpers for building Spin `wasi-http` components.
5858
pub mod http;
5959

60-
/// MQTT messaging.
6160
#[allow(missing_docs)]
62-
pub mod mqtt {
63-
pub use super::wit::v2::mqtt::{Connection, Error, Payload, Qos};
64-
}
61+
pub mod mqtt;
6562

6663
/// Redis storage and messaging.
6764
#[allow(missing_docs)]

src/mqtt.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! MQTT message publishing.
2+
//!
3+
//! To receive MQTT messages, use the MQTT trigger.
4+
//!
5+
//! # Examples
6+
//!
7+
//! Send an MQTT message.
8+
//!
9+
//! ```no_run
10+
//! use spin_sdk::mqtt::{Connection, Qos};
11+
//!
12+
//! # fn ensure_pet_picture(_: &[u8]) -> anyhow::Result<()> { Ok(()) }
13+
//! # fn use_mqtt(request: spin_sdk::http::Request) -> anyhow::Result<()> {
14+
//! let user = spin_sdk::variables::get("mqtt_username")?;
15+
//! let password = spin_sdk::variables::get("mqtt_password")?;
16+
//!
17+
//! let conn = Connection::open(
18+
//! "mqtt://localhost:1883?client_id=123",
19+
//! &user,
20+
//! &password,
21+
//! 30 /* seconds */
22+
//! )?;
23+
//!
24+
//! let payload = request.body().to_vec();
25+
//! ensure_pet_picture(&payload)?;
26+
//!
27+
//! conn.publish("pet-pictures", &payload, Qos::AtLeastOnce)?;
28+
//! # Ok(())
29+
//! # }
30+
//! ```
31+
32+
/// An open connection to an MQTT queue.
33+
///
34+
/// The address must be in URL form, and must include a `client_id`:
35+
/// `mqtt://hostname?client_id=...`
36+
///
37+
/// # Examples
38+
///
39+
/// Send an MQTT message.
40+
///
41+
/// ```no_run
42+
/// use spin_sdk::mqtt::{Connection, Qos};
43+
///
44+
/// # fn ensure_pet_picture(_: &[u8]) -> anyhow::Result<()> { Ok(()) }
45+
/// # fn use_mqtt(request: spin_sdk::http::Request) -> anyhow::Result<()> {
46+
/// let user = spin_sdk::variables::get("mqtt_username")?;
47+
/// let password = spin_sdk::variables::get("mqtt_password")?;
48+
///
49+
/// let conn = Connection::open(
50+
/// "mqtt://localhost:1883?client_id=123",
51+
/// &user,
52+
/// &password,
53+
/// 30 /* seconds */
54+
/// )?;
55+
///
56+
/// let payload = request.body().to_vec();
57+
/// ensure_pet_picture(&payload)?;
58+
///
59+
/// conn.publish("pet-pictures", &payload, Qos::AtLeastOnce)?;
60+
/// # Ok(())
61+
/// # }
62+
/// ```
63+
pub use super::wit::v2::mqtt::Connection;
64+
65+
pub use super::wit::v2::mqtt::{Error, Payload, Qos};

0 commit comments

Comments
 (0)