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_client::tokio::{ClientOptions, MqttClient, Subscription, TcpTransport};
use finmqtt_core::wire::QoS;
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 outcomes = 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: ConnectionStateWatchImplementations§
Source§impl MqttClient
impl MqttClient
Sourcepub const fn builder<T>(
transport: T,
options: ClientOptions,
) -> ClientBuilder<T>
pub const fn builder<T>( transport: T, options: ClientOptions, ) -> ClientBuilder<T>
Starts a ClientBuilder from ClientOptions.
Use the builder to set persistence and channel capacities, then call
start. Without those, the client keeps the
session state only in memory and uses default channel capacities.
Sourcepub fn publish<P>(
&self,
topic: TopicBuf,
payload: P,
qos: QoS,
) -> PublishBuilder
pub fn publish<P>( &self, topic: TopicBuf, payload: P, 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.
Sourcepub fn subscribe(&self, subscription: Subscription) -> SubscribeBuilder
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.
Sourcepub fn unsubscribe(&self, filter: TopicFilterBuf) -> UnsubscribeBuilder
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.
Sourcepub const fn connection_status(&self) -> &ConnectionStateWatch
pub const fn connection_status(&self) -> &ConnectionStateWatch
Returns a handle for observing the connection status.
The returned ConnectionStateWatch provides:
current: synchronous snapshot of the currentConnectionStatus.wait_until_connected: async, resolves once the client reachesConnected.changes: a [futures::Stream] that emits each status transition.
Clone the returned reference if you need a 'static copy.
Sourcepub async fn wait_for_shutdown(&self)
pub async fn wait_for_shutdown(&self)
Waits until the event loop task has fully exited.
The counterpart to
EventLoopHandle for
tasks that only have a client handle. It resolves when the status
channel reports that the event loop task has exited for any reason.
Returns immediately if the client has already shut down.
Trait Implementations§
Source§impl Clone for MqttClient
impl Clone for MqttClient
Source§fn clone(&self) -> MqttClient
fn clone(&self) -> MqttClient
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for MqttClient
impl RefUnwindSafe for MqttClient
impl Send for MqttClient
impl Sync for MqttClient
impl Unpin for MqttClient
impl UnsafeUnpin for MqttClient
impl UnwindSafe for MqttClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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