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: ConnectionStateWatchImplementations§
§impl MqttClient
impl MqttClient
pub const fn builder<T: Transport + Send + Sync + 'static>(
transport: T,
options: ClientOptions,
) -> ClientBuilder<T>
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)
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
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
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
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
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.
pub 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 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
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 moreAuto Trait Implementations§
impl Freeze for MqttClient
impl RefUnwindSafe for MqttClient
impl Send for MqttClient
impl Sync for MqttClient
impl Unpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> 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