pub struct AsyncClient { /* private fields */ }
Expand description

An asynchronous MQTT connection client.

Implementations§

source§

impl AsyncClient

source

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.

source

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).

source

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.

source

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 be None, in which case the default options are used.
source

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 options
  • success_cb The callback for a successful connection.
  • failure_cb The callback for a failed connection attempt.
source

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.

source

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.
source

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.

source

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.

source

pub fn is_connected(&self) -> bool

Determines if this client is currently connected to an MQTT broker.

source

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.
source

pub fn remove_connected_callback(&self)

Removes the callback for when the conection is established

source

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.
source

pub fn remove_connection_lost_callback(&self)

Removes the callback for when the connection is lost

source

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.
source

pub fn remove_disconnected_callback(&self)

Removes the callback for when a disconnect message is received from the broker.

source

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.
source

pub fn remove_message_callback(&self)

Removes the callback for when a message arrives from the broker.

source

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.

source

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.

source

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

source

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

source

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

source

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

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn stop_consuming(&self)

Stops the client from consuming messages.

source

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.

source

pub fn stop_stream(&self)

Stops the client from streaming messages in.

source

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

source

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

source§

fn clone(&self) -> AsyncClient

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Send for AsyncClient

source§

impl Sync for AsyncClient

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.