pub struct Client { /* private fields */ }
Expand description
A high-level, asynchronous mosquitto MQTT client
Implementations§
Source§impl Client
impl Client
Sourcepub fn with_id(id: &str, clean_session: bool) -> Result<Self, Error>
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.
Sourcepub fn with_auto_id() -> Result<Self, Error>
pub fn with_auto_id() -> Result<Self, Error>
Create a new client instance with a random client id
Sourcepub fn set_username_and_password(
&self,
username: Option<&str>,
password: Option<&str>,
) -> Result<(), Error>
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.
Sourcepub async fn connect(
&self,
host: &str,
port: c_int,
keep_alive_interval: Duration,
bind_address: Option<&str>,
) -> Result<ConnectionStatus, Error>
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.
Sourcepub async fn publish<T: AsRef<str>, P: AsRef<[u8]>>(
&self,
topic: T,
payload: P,
qos: QoS,
retain: bool,
) -> Result<MessageId, Error>
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.
Sourcepub fn set_last_will<T: AsRef<str>, P: AsRef<[u8]>>(
&self,
topic: T,
payload: P,
qos: QoS,
retain: bool,
) -> Result<(), Error>
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.
Sourcepub fn clear_last_will(&self) -> Result<(), Error>
pub fn clear_last_will(&self) -> Result<(), Error>
Remove a previously configured will. This must be called before calling connect
Sourcepub fn subscriber(&self) -> Option<Receiver<Event>>
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.
Sourcepub async fn subscribe(&self, pattern: &str, qos: QoS) -> Result<(), Error>
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.
Sourcepub async fn unsubscribe(&self, pattern: &str) -> Result<(), Error>
pub async fn unsubscribe(&self, pattern: &str) -> Result<(), Error>
Remove subscription(s) for topics that match pattern
.
Sourcepub fn set_option(&self, option: &ClientOption<'_>) -> Result<(), Error>
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.
Sourcepub 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>
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>
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.
Sourcepub fn set_reconnect_delay(
&self,
reconnect_delay: Duration,
max_reconnect_delay: Duration,
use_exponential_backoff: bool,
) -> Result<(), Error>
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.