pub struct MqttManager { /* private fields */ }
Expand description
The main MQTT manager struct
Implementations§
Source§impl MqttManager
impl MqttManager
Sourcepub fn new(host_url: &str) -> MqttManager
pub fn new(host_url: &str) -> MqttManager
Create a new MqttManager from a host URL in the form:
- mqtt://localhost:1883?client_id=client2
Sourcepub async fn disconnect(&mut self)
pub async fn disconnect(&mut self)
Sourcepub async fn publish<T, U>(&mut self, topic: T, payload: U, qos: u8)
pub async fn publish<T, U>(&mut self, topic: T, payload: U, qos: u8)
Publish data to a topic.
topic is the topic name, payload is the payload bytes, and qos is the MQTT qos in the range 0-2
§panic
This panics if the qos is invalid or if there is an error publishing.
Sourcepub async fn subscribe<T: Into<String>>(
&mut self,
topic: T,
qos: u8,
callback: CallbackFn,
)
pub async fn subscribe<T: Into<String>>( &mut self, topic: T, qos: u8, callback: CallbackFn, )
Subscribe to a topic.
topic is the subscription topic including optional wildcards per the MQTT spec.
qos is the subscription qos in the range 0-3. callback is a callback async function
which should be wrapped by calling crate::make_callback
.
§panic
This panics if the qos is invalid or if there is an error subscribing.
Sourcepub async fn process(&mut self) -> Result<(), ConnectionError>
pub async fn process(&mut self) -> Result<(), ConnectionError>
Wait for a single packet and process reconnects, pings, etc.
This should be called reguarly, either in an event loop or in a background thread using tokio::spawn.
If a SUBSCRIBE packet is returned and there’s a registered callback it will be await’d.