MqttClient

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

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

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

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>

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>

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>

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

§

fn clone(&self) -> MqttClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.