Struct paho_mqtt::async_client::AsyncClient
source · pub struct AsyncClient { /* private fields */ }
Expand description
An asynchronous MQTT connection client.
Implementations§
source§impl 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 most recent MQTT version for the client.
This is the version of the current connection, or the most recent connection if currently disconnected. Before an initial connection is made, this will report MQTT_VERSION_DEFAULT (0).
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) -> ConnectTokenwhere
T: Into<Option<ConnectOptions>>,
pub fn connect<T>(&self, opts: T) -> ConnectTokenwhere 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
) -> ConnectTokenwhere
FS: Fn(&AsyncClient, u16) + Send + 'static,
FF: Fn(&AsyncClient, u16, i32) + Send + 'static,
pub fn connect_with_callbacks<FS, FF>( &self, opts: ConnectOptions, success_cb: FS, failure_cb: FF ) -> ConnectTokenwhere FS: Fn(&AsyncClient, u16) + Send + 'static, FF: Fn(&AsyncClient, u16, i32) + Send + '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
) -> ConnectTokenwhere
FS: Fn(&AsyncClient, u16) + Send + 'static,
FF: Fn(&AsyncClient, u16, i32) + Send + 'static,
pub fn reconnect_with_callbacks<FS, FF>( &self, success_cb: FS, failure_cb: FF ) -> ConnectTokenwhere FS: Fn(&AsyncClient, u16) + Send + 'static, FF: Fn(&AsyncClient, u16, i32) + Send + '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 ⓘwhere
T: Into<Option<DisconnectOptions>>,
pub fn disconnect<T>(&self, opt_opts: T) -> Token ⓘ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 ⓘ
pub fn disconnect_after(&self, timeout: Duration) -> Token ⓘ
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) + Send + 'static,
pub fn set_connected_callback<F>(&self, cb: F)where F: FnMut(&AsyncClient) + Send + '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) + Send + 'static,
pub fn set_connection_lost_callback<F>(&self, cb: F)where F: FnMut(&AsyncClient) + Send + '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) + Send + 'static,
pub fn set_disconnected_callback<F>(&self, cb: F)where F: FnMut(&AsyncClient, Properties, ReasonCode) + Send + '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>) + Send + 'static,
pub fn set_message_callback<F>(&self, cb: F)where F: FnMut(&AsyncClient, Option<Message>) + Send + '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 ⓘ
pub fn publish(&self, msg: Message) -> DeliveryToken ⓘ
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) -> SubscribeTokenwhere
S: Into<String>,
pub fn subscribe<S>(&self, topic: S, qos: i32) -> SubscribeTokenwhere 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
) -> SubscribeTokenwhere
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 ) -> SubscribeTokenwhere 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]) -> SubscribeManyTokenwhere
T: AsRef<str>,
pub fn subscribe_many<T>(&self, topics: &[T], qos: &[i32]) -> SubscribeManyTokenwhere 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
) -> SubscribeManyTokenwhere
T: AsRef<str>,
P: Into<Option<Properties>>,
pub fn subscribe_many_with_options<T, P>( &self, topics: &[T], qos: &[i32], opts: &[SubscribeOptions], props: P ) -> SubscribeManyTokenwhere 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 ⓘwhere
S: Into<String>,
pub fn unsubscribe<S>(&self, topic: S) -> Token ⓘ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 ⓘwhere
S: Into<String>,
pub fn unsubscribe_with_options<S>(&self, topic: S, props: Properties) -> Token ⓘ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 ⓘwhere
T: AsRef<str>,
pub fn unsubscribe_many<T>(&self, topics: &[T]) -> Token ⓘ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 ⓘwhere
T: AsRef<str>,
pub fn unsubscribe_many_with_options<T>( &self, topics: &[T], props: Properties ) -> Token ⓘ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<L>(&mut self, buffer_lim: L) -> AsyncReceiver<Option<Message>>where
L: Into<Option<usize>>,
pub fn get_stream<L>(&mut self, buffer_lim: L) -> AsyncReceiver<Option<Message>>where L: Into<Option<usize>>,
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.
The stream will rely on a bounded channel with the given buffer capacity if ‘buffer_sz’ is ‘Some’ or will rely on an unbounded channel if ‘buffer_sz’ is ‘None’.
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.
sourcepub fn client_id(&self) -> String
pub fn client_id(&self) -> String
Returns client ID used for client instance
Client ID is returned as a rust String as set in a CreateOptionsBuilder for symmetry
sourcepub fn server_uri(&self) -> String
pub fn server_uri(&self) -> String
Returns server URI used for connection
Server URI is returned as a rust String as set in a CreateOptionsBuilder for symmetry
Trait Implementations§
source§impl Clone for AsyncClient
impl Clone for AsyncClient
source§fn clone(&self) -> AsyncClient
fn clone(&self) -> AsyncClient
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more