Struct mosquitto_rs::Client

source ·
pub struct Client { /* private fields */ }
Expand description

A high-level, asynchronous mosquitto MQTT client

Implementations§

source§

impl Client

source

pub fn with_id(id: &str, clean_session: bool) -> Result<Self, Error>

Create a new client instance with the specified id. If clean_session is true, instructs the broker to clean all messages and subscriptions on disconnect. Otherwise it will preserve them.

source

pub fn with_auto_id() -> Result<Self, Error>

Create a new client instance with a random client id

source

pub fn set_username_and_password( &self, username: Option<&str>, password: Option<&str> ) -> Result<(), Error>

Configure the client with an optional username and password. The default is None for both. Whether you need to configure these credentials depends on the broker configuration.

source

pub async fn connect( &self, host: &str, port: c_int, keep_alive_interval: Duration, bind_address: Option<&str> ) -> Result<ConnectionStatus, Error>

Connect to the broker on the specified host and port. port is typically 1883 for mqtt, but it may be different in your environment.

keep_alive_interval specifies the interval at which keepalive requests are sent. mosquitto has a minimum value of 5 seconds for this and will generate an error if you use a smaller value.

bind_address can be used to specify the outgoing interface for the connection.

connect completes when the broker acknowledges the CONNECT command.

Yields the connection return code; if the status was rejected, then an Error::RejectedConnection() variant will be returned so that you don’t have to manually check the success.

source

pub async fn publish<T: AsRef<str>, P: AsRef<[u8]>>( &self, topic: T, payload: P, qos: QoS, retain: bool ) -> Result<MessageId, Error>

Publish a message to the specified topic.

The payload size can be 0-283, 435 or 455 bytes; other values will generate an error result.

retain will set the message to be retained by the broker, and delivered to new subscribers.

Returns the assigned MessageId value for the publish.

source

pub fn set_last_will<T: AsRef<str>, P: AsRef<[u8]>>( &self, topic: T, payload: P, qos: QoS, retain: bool ) -> Result<(), Error>

Configure will information for a mosquitto instance. By default, clients do not have a will. This must be called before calling connect.

The payload size can be 0-283, 435 or 455 bytes; other values will generate an error result.

retain will set the message to be retained by the broker, and delivered to new subscribers.

source

pub fn clear_last_will(&self) -> Result<(), Error>

Remove a previously configured will. This must be called before calling connect

source

pub fn subscriber(&self) -> Option<Receiver<Event>>

Returns a channel that yields messages from topics that this client has subscribed to. This method can be called only once; the first time it returns the channel and subsequently it no longer has the channel receiver to retur, so will yield None.

source

pub async fn subscribe(&self, pattern: &str, qos: QoS) -> Result<(), Error>

Establish a subscription to topics matching pattern. The messages will be delivered via the channel returned via the subscriber method.

source

pub async fn unsubscribe(&self, pattern: &str) -> Result<(), Error>

Remove subscription(s) for topics that match pattern.

source

pub fn set_option(&self, option: &ClientOption<'_>) -> Result<(), Error>

Set an option for the client. Most options need to be set prior to calling connect in order to have any effect.

source

pub fn configure_tls<CAFILE, CAPATH, CERTFILE, KEYFILE>( &self, ca_file: Option<CAFILE>, ca_path: Option<CAPATH>, cert_file: Option<CERTFILE>, key_file: Option<KEYFILE>, pw_callback: Option<PasswdCallback> ) -> Result<(), Error>
where CAFILE: AsRef<Path>, CAPATH: AsRef<Path>, CERTFILE: AsRef<Path>, KEYFILE: AsRef<Path>,

Configures the TLS parameters for the client.

ca_file is the path to a PEM encoded trust CA certificate file. Either ca_file or ca_path must be set.

ca_path is the path to a directory containing PEM encoded trust CA certificates. Either ca_file or ca_path must be set.

cert_file path to a file containing the PEM encoded certificate file for this client. If None then key_file must also be None and no client certificate will be used.

key_file path to a file containing the PEM encoded private key for this client. If None them cert_file must also be None and no client certificate will be used.

pw_callback allows you to provide a password to decrypt an encrypted key file. Specify None if the key file isn’t password protected.

source

pub fn set_reconnect_delay( &self, reconnect_delay: Duration, max_reconnect_delay: Duration, use_exponential_backoff: bool ) -> Result<(), Error>

Controls reconnection behavior when running in the message loop. By default, if a client is unexpectedly disconnected, mosquitto will try to reconnect. The default reconnect parameters are to retry once per second to reconnect.

You change adjust the delay between connection attempts by changing the parameters with this function.

reconnect_delay is the base delay amount.

If use_exponential_backoff is true, then the delay is doubled on each successive attempt, until the max_reconnect_delay is reached.

If use_exponential_backoff is false, then the reconnect_delay is added on each successive attempt, until the max_reconnect_delay is reached.

Trait Implementations§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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 T
where 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.