Struct paho_mqtt::async_client::AsyncClient
source · [−]pub struct AsyncClient { /* private fields */ }
Expand description
An asynchronous MQTT connection client.
Implementations
sourceimpl AsyncClient
impl AsyncClient
sourcepub fn new<T>(opts: T) -> Result<AsyncClient> where
T: Into<CreateOptions>,
pub fn new<T>(opts: T) -> Result<AsyncClient> where
T: Into<CreateOptions>,
Creates a new MQTT client which can connect to an MQTT broker.
Arguments
opts
The create options for the client.
sourcepub fn mqtt_version(&self) -> u32
pub fn mqtt_version(&self) -> u32
Gets the MQTT version for which the client was created.
This is the default version used when connecting, unless a specific value is set in the connect options. It is typically the highest version that can be used by the client. Specifically, if the client is created for v3, then it can not be used to connect with the v5 protocol.
sourcepub fn current_mqtt_version(&self) -> u32
pub fn current_mqtt_version(&self) -> u32
The current version of the protol being used, if the client is connected.
sourcepub fn user_data(&self) -> Option<&UserData>
pub fn user_data(&self) -> Option<&UserData>
Get access to the user-defined data in the client.
This returns a reference to a read/write lock around the user data so that the application can access the data, as needed from any outside thread or a callback.
Note that it’s up to the application to ensure that it doesn’t deadlock the callback thread when accessing the user data.
sourcepub fn connect<T>(&self, opts: T) -> ConnectToken where
T: Into<Option<ConnectOptions>>,
pub fn connect<T>(&self, opts: T) -> ConnectToken where
T: Into<Option<ConnectOptions>>,
Connects to an MQTT broker using the specified connect options.
Arguments
opts
The connect options. This can beNone
, in which case the default options are used.
sourcepub fn connect_with_callbacks<FS, FF>(
&self,
opts: ConnectOptions,
success_cb: FS,
failure_cb: FF
) -> ConnectToken where
FS: Fn(&AsyncClient, u16) + 'static,
FF: Fn(&AsyncClient, u16, i32) + 'static,
pub fn connect_with_callbacks<FS, FF>(
&self,
opts: ConnectOptions,
success_cb: FS,
failure_cb: FF
) -> ConnectToken where
FS: Fn(&AsyncClient, u16) + 'static,
FF: Fn(&AsyncClient, u16, i32) + 'static,
Connects to an MQTT broker using the specified connect options.
Arguments
opts
The connect optionssuccess_cb
The callback for a successful connection.failure_cb
The callback for a failed connection attempt.
sourcepub fn reconnect(&self) -> ConnectToken
pub fn reconnect(&self) -> ConnectToken
Attempts to reconnect to the broker. This can only be called after a connection was initially made or attempted. It will retry with the same connect options.
sourcepub fn reconnect_with_callbacks<FS, FF>(
&self,
success_cb: FS,
failure_cb: FF
) -> ConnectToken where
FS: Fn(&AsyncClient, u16) + 'static,
FF: Fn(&AsyncClient, u16, i32) + 'static,
pub fn reconnect_with_callbacks<FS, FF>(
&self,
success_cb: FS,
failure_cb: FF
) -> ConnectToken where
FS: Fn(&AsyncClient, u16) + 'static,
FF: Fn(&AsyncClient, u16, i32) + 'static,
Attempts to reconnect to the broker, using callbacks to signal completion. This can only be called after a connection was initially made or attempted. It will retry with the same connect options.
Arguments
success_cb
The callback for a successful connection.failure_cb
The callback for a failed connection attempt.
sourcepub fn disconnect<T>(&self, opt_opts: T) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
T: Into<Option<DisconnectOptions>>,
pub fn disconnect<T>(&self, opt_opts: T) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
T: Into<Option<DisconnectOptions>>,
Disconnects from the MQTT broker.
Arguments
opt_opts
Optional disconnect options. Specifying None
will use
default of immediate (zero timeout) disconnect.
sourcepub fn disconnect_after(&self, timeout: Duration) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
pub fn disconnect_after(&self, timeout: Duration) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
Disconnect from the MQTT broker with a timeout. This will delay the disconnect for up to the specified timeout to allow in-flight messages to complete. This is the same as calling disconnect with options specifying a timeout.
Arguments
timeout
The amount of time to wait for the disconnect. This has
a resolution in milliseconds.
sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Determines if this client is currently connected to an MQTT broker.
sourcepub fn set_connected_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient) + 'static,
pub fn set_connected_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient) + 'static,
Sets the callback for when the connection is established with the broker.
Arguments
cb
The callback to register with the library. This can be a function or a closure.
sourcepub fn remove_connected_callback(&self)
pub fn remove_connected_callback(&self)
Removes the callback for when the conection is established
sourcepub fn set_connection_lost_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient) + 'static,
pub fn set_connection_lost_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient) + 'static,
Sets the callback for when the connection is lost with the broker.
Arguments
cb
The callback to register with the library. This can be a function or a closure.
sourcepub fn remove_connection_lost_callback(&self)
pub fn remove_connection_lost_callback(&self)
Removes the callback for when the connection is lost
sourcepub fn set_disconnected_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient, Properties, ReasonCode) + 'static,
pub fn set_disconnected_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient, Properties, ReasonCode) + 'static,
Sets the callback for when a disconnect message arrives from the broker.
Arguments
cb
The callback to register with the library. This can be a function or a closure.
sourcepub fn remove_disconnected_callback(&self)
pub fn remove_disconnected_callback(&self)
Removes the callback for when a disconnect message is received from the broker.
sourcepub fn set_message_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient, Option<Message>) + 'static,
pub fn set_message_callback<F>(&self, cb: F) where
F: FnMut(&AsyncClient, Option<Message>) + 'static,
Sets the callback for when a message arrives from the broker.
Arguments
cb
The callback to register with the library. This can be a function or a closure.
sourcepub fn remove_message_callback(&self)
pub fn remove_message_callback(&self)
Removes the callback for when a message arrives from the broker.
sourcepub fn try_publish(&self, msg: Message) -> Result<DeliveryToken>
pub fn try_publish(&self, msg: Message) -> Result<DeliveryToken>
Attempts to publish a message to the MQTT broker, but returns an error immediately if there’s a problem creating or queuing the message.
Returns a Publish Error on failure so that the original message can be recovered and sent again.
sourcepub fn publish(&self, msg: Message) -> DeliveryTokenⓘNotable traits for DeliveryTokenimpl Future for DeliveryToken type Output = Result<()>;
pub fn publish(&self, msg: Message) -> DeliveryTokenⓘNotable traits for DeliveryTokenimpl Future for DeliveryToken type Output = Result<()>;
Publishes a message to the MQTT broker.
Returns a Delivery Token to track the progress of the operation.
sourcepub fn subscribe<S>(&self, topic: S, qos: i32) -> SubscribeToken where
S: Into<String>,
pub fn subscribe<S>(&self, topic: S, qos: i32) -> SubscribeToken where
S: Into<String>,
Subscribes to a single topic.
Arguments
topic
The topic name
qos
The quality of service requested for messages
sourcepub fn subscribe_with_options<S, T, P>(
&self,
topic: S,
qos: i32,
opts: T,
props: P
) -> SubscribeToken where
S: Into<String>,
T: Into<SubscribeOptions>,
P: Into<Option<Properties>>,
pub fn subscribe_with_options<S, T, P>(
&self,
topic: S,
qos: i32,
opts: T,
props: P
) -> SubscribeToken where
S: Into<String>,
T: Into<SubscribeOptions>,
P: Into<Option<Properties>>,
Subscribes to a single topic with v5 options
Arguments
topic
The topic name
qos
The quality of service requested for messages
opts
Options for the subscription
props
MQTT v5 properties
sourcepub fn subscribe_many<T>(&self, topics: &[T], qos: &[i32]) -> SubscribeManyToken where
T: AsRef<str>,
pub fn subscribe_many<T>(&self, topics: &[T], qos: &[i32]) -> SubscribeManyToken where
T: AsRef<str>,
Subscribes to multiple topics simultaneously.
Arguments
topics
The collection of topic names
qos
The quality of service requested for messages
sourcepub fn subscribe_many_with_options<T, P>(
&self,
topics: &[T],
qos: &[i32],
opts: &[SubscribeOptions],
props: P
) -> SubscribeManyToken where
T: AsRef<str>,
P: Into<Option<Properties>>,
pub fn subscribe_many_with_options<T, P>(
&self,
topics: &[T],
qos: &[i32],
opts: &[SubscribeOptions],
props: P
) -> SubscribeManyToken where
T: AsRef<str>,
P: Into<Option<Properties>>,
Subscribes to multiple topics simultaneously with options.
Arguments
topics
The collection of topic names
qos
The quality of service requested for messages
opts
Subscribe options (one per topic)
props
MQTT v5 properties
sourcepub fn unsubscribe<S>(&self, topic: S) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
S: Into<String>,
pub fn unsubscribe<S>(&self, topic: S) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
S: Into<String>,
Unsubscribes from a single topic.
Arguments
topic
The topic to unsubscribe. It must match a topic from a
previous subscribe.
sourcepub fn unsubscribe_with_options<S>(&self, topic: S, props: Properties) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
S: Into<String>,
pub fn unsubscribe_with_options<S>(&self, topic: S, props: Properties) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
S: Into<String>,
Unsubscribes from a single topic.
Arguments
topic
The topic to unsubscribe. It must match a topic from a
previous subscribe.
props
MQTT v5 properties for the unsubscribe.
sourcepub fn unsubscribe_many<T>(&self, topics: &[T]) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
T: AsRef<str>,
pub fn unsubscribe_many<T>(&self, topics: &[T]) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
T: AsRef<str>,
Unsubscribes from multiple topics simultaneously.
Arguments
topic
The topics to unsubscribe. Each must match a topic from a
previous subscribe.
sourcepub fn unsubscribe_many_with_options<T>(
&self,
topics: &[T],
props: Properties
) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
T: AsRef<str>,
pub fn unsubscribe_many_with_options<T>(
&self,
topics: &[T],
props: Properties
) -> TokenⓘNotable traits for Tokenimpl Future for Token type Output = Result<ServerResponse>;
where
T: AsRef<str>,
Unsubscribes from multiple topics simultaneously.
Arguments
topic
The topics to unsubscribe. Each must match a topic from a
previous subscribe.
props
MQTT v5 properties for the unsubscribe.
sourcepub fn start_consuming(&self) -> Receiver<Option<Message>>
pub fn start_consuming(&self) -> Receiver<Option<Message>>
Starts the client consuming messages for a blocking (non-async) app.
This starts the client receiving messages and placing them into an mpsc queue. It returns the receiving-end of the queue for the application to get the messages. This can be called at any time after the client is created, but it should be called before subscribing to any topics, otherwise messages can be lost.
sourcepub fn stop_consuming(&self)
pub fn stop_consuming(&self)
Stops the client from consuming messages.
sourcepub fn get_stream(&mut self, buffer_sz: usize) -> AsyncReceiver<Option<Message>>
pub fn get_stream(&mut self, buffer_sz: usize) -> AsyncReceiver<Option<Message>>
Creates a futures stream for consuming messages.
This will install an internal callback to receive the incoming
messages from the client, and return the receive side of the channel.
The stream will stay open for the life of the client. If the client
gets disconnected, it will insert None
into the channel to signal
the app about the disconnect.
It’s a best practice to open the stream before connecting to the server. When using persistent (non-clean) sessions, messages could arriving as soon as the connection is made - even before the connect() call returns.
sourcepub fn stop_stream(&self)
pub fn stop_stream(&self)
Stops the client from streaming messages in.
Trait Implementations
sourceimpl Clone for AsyncClient
impl Clone for AsyncClient
sourcefn clone(&self) -> AsyncClient
fn clone(&self) -> AsyncClient
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Send for AsyncClient
impl Sync for AsyncClient
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more