Struct MqttClient
pub struct MqttClient {
cmd_tx: Sender<Command>,
}Expand description
A session-oriented MQTT v5 client.
Cloning is cheap — every clone shares the same underlying session. Operations (publish, subscribe, …) can be called at any time, regardless of connection state. Messages are queued while disconnected and sent once a connection is (re-)established.
§Example
use finmqtt_core::wire::QoS;
use finmqtt_tokio::transport::TcpTransport;
use finmqtt_tokio::{ClientOptions, MqttClient, MqttString, TopicFilterBuf};
use futures::StreamExt;
let transport = TcpTransport::new("broker.example.com", 1883);
let options = ClientOptions::new("my-client".parse()?).reconnect();
let (client, mut messages) = MqttClient::new(transport, options).await?;
// Subscribe is a pure protocol operation.
let reason_codes = client
.subscribe("sensors/#".parse()?)
.qos(QoS::AtLeastOnce)
.send()
.await?;
// All messages arrive on the single message stream.
while let Some(msg) = messages.next().await {
println!("{}: {:?}", msg.topic, msg.payload);
}Fields§
§cmd_tx: Sender<Command>Implementations§
§impl MqttClient
impl MqttClient
pub async fn new<T: Transport + Send + Sync + 'static>(
transport: T,
options: ClientOptions,
) -> Result<(Self, MessageStream)>
pub async fn new<T: Transport + Send + Sync + 'static>( transport: T, options: ClientOptions, ) -> Result<(Self, MessageStream)>
Create a new client and start connecting in the background.
Returns the client handle and a MessageStream that yields every
incoming PUBLISH message. The stream ends when the client is
disconnected and no reconnect is configured.
§Errors
Returns an I/O error if a journal path is configured and the journal file cannot be read or opened for writing.
pub fn publish(
&self,
topic: TopicBuf,
payload: impl Into<Vec<u8>>,
) -> PublishBuilder<'_>
pub fn publish( &self, topic: TopicBuf, payload: impl Into<Vec<u8>>, ) -> PublishBuilder<'_>
Start building a publish operation.
Returns a PublishBuilder that can be configured with QoS, retain,
and v5 properties before calling send.
pub const fn subscribe(&self, filter: TopicFilterBuf) -> SubscribeBuilder<'_>
pub const fn subscribe(&self, filter: TopicFilterBuf) -> SubscribeBuilder<'_>
Start building a subscribe operation.
Returns a SubscribeBuilder that can be configured with QoS and
v5 subscription options before calling
send.
pub async fn subscribe_many(
&self,
filters: Vec<(TopicFilterBuf, SubscriptionOptions)>,
subscription_id: Option<u32>,
) -> Result<Vec<ReasonCode>, Error>
pub async fn subscribe_many( &self, filters: Vec<(TopicFilterBuf, SubscriptionOptions)>, subscription_id: Option<u32>, ) -> Result<Vec<ReasonCode>, Error>
Subscribe to multiple topic filters in a single SUBSCRIBE packet.
Each entry pairs a filter with its SubscriptionOptions. An
optional subscription identifier can be attached to the whole batch.
Returns one reason code per filter from the SUBACK.
§Errors
Returns Error::ConnectionClosed if the connection is lost before
SUBACK arrives.
pub async fn unsubscribe(
&self,
filters: Vec<TopicFilterBuf>,
) -> Result<Vec<ReasonCode>, Error>
pub async fn unsubscribe( &self, filters: Vec<TopicFilterBuf>, ) -> Result<Vec<ReasonCode>, Error>
Unsubscribe from one or more topic filters.
§Errors
Returns an error if the connection is lost before UNSUBACK arrives.
pub async fn disconnect(&self) -> Result<(), Error>
pub async fn disconnect(&self) -> Result<(), Error>
Send a graceful DISCONNECT and shut down the session.
§Errors
Returns Error::ConnectionClosed if the connection is already gone.
Trait Implementations§
§impl Clone for MqttClient
impl Clone for MqttClient
§fn clone(&self) -> MqttClient
fn clone(&self) -> MqttClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more