MqttClient

Struct MqttClient 

pub struct MqttClient {
    cmd_tx: Sender<Command>,
    status: ConnectionStateWatch,
}
Expand description

A session-oriented MQTT v5 client.

Cloning is cheap; every clone shares the same underlying session. Operations (publish, subscribe, unsubscribe) 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_tokio::transport::TcpTransport;
use finmqtt_tokio::{ClientOptions, MqttClient, QoS, Subscription};
use futures::StreamExt;

let transport = TcpTransport::new("broker.example.com", 1883);
let options = ClientOptions::new("my-client".parse()?).reconnect();

let (owner, client, mut messages) = MqttClient::builder(transport, options).start().await?;

// Subscribe is a pure protocol operation.
let reason_codes = client
    .subscribe(Subscription::new("sensors/#".parse()?, QoS::AtLeastOnce))
    .send()
    .await?;

// All messages arrive on the single message stream.
while let Some(msg) = messages.next().await {
    println!("{}: {:?}", msg.topic, msg.payload);
}

owner.stop().await;

Fields§

§cmd_tx: Sender<Command>§status: ConnectionStateWatch

Implementations§

§

impl MqttClient

pub const fn builder<T: Transport + Send + Sync + 'static>( transport: T, options: ClientOptions, ) -> ClientBuilder<T>

Starts a ClientBuilder from the given ClientOptions.

Use the builder to set persistence and channel capacities, then call start. Without those, the client stores session state only in memory and uses default channel capacities.

pub(crate) fn spawn<T: Transport + Send + Sync + 'static>( transport: T, options: ClientOptions, clean_start: bool, session: JournaledSession, store: Persistence, command_channel_capacity: usize, message_channel_capacity: usize, ) -> (EventLoopHandle, Self, MessageStream)

Spawns the connection event loop and returns its owner handle, the client handle, and the message stream.

pub fn publish( &self, topic: TopicBuf, payload: impl Into<Vec<u8>>, qos: QoS, ) -> PublishBuilder

Starts building a publish operation.

Returns a PublishBuilder that can be configured with retain and v5 properties. Call send to get a PublishFuture that drives the publish to broker acknowledgement, or PublishFuture::queued to return as soon as the message is stored locally.

pub fn subscribe(&self, subscription: Subscription) -> SubscribeBuilder

Starts building a subscribe operation.

Additional subscriptions can be batched with SubscribeBuilder::and. Call send to get a SubscribeFuture that drives to broker acknowledgement (SUBACK), or call SubscribeFuture::queued to return once the subscription is recorded locally.

pub fn unsubscribe(&self, filter: TopicFilterBuf) -> UnsubscribeBuilder

Starts building an unsubscribe operation.

Additional filters can be added with UnsubscribeBuilder::and. Call send to get an UnsubscribeFuture.

pub const fn connection_status(&self) -> &ConnectionStateWatch

Returns a handle for observing the connection status.

The returned ConnectionStateWatch provides:

Clone the returned reference if you need a 'static copy.

pub async fn wait_for_shutdown(&self)

Waits until the event loop task has fully exited.

The counterpart to EventLoopHandle for tasks that only hold a client handle: it resolves once the event loop is gone (stopped, owner dropped, or exited on its own), detected through the status channel. Returns immediately if the client has already shut down.

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,