|
| 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