Crate mosquitto_rs
source ·Expand description
This crate implements an async MQTT client using libmosquitto.
use mosquitto_rs::*;
fn main() -> Result<(), Error> {
smol::block_on(async {
let mut client = Client::with_auto_id()?;
let rc = client.connect(
"localhost", 1883,
std::time::Duration::from_secs(5), None).await?;
println!("connect: {}", rc);
let subscriptions = client.subscriber().unwrap();
client.subscribe("test", QoS::AtMostOnce).await?;
println!("subscribed");
client.publish("test", b"woot", QoS::AtMostOnce, false)
.await?;
println!("published");
if let Ok(msg) = subscriptions.recv().await {
println!("msg: {:?}", msg);
}
Ok(())
})
}
Features
The following feature flags are available:
router
- include the router module andMqttRouter
type. This is on by default.vendored-mosquitto
- use bundled libmosquitto 2.4 library. This is on by default.vendored-mosquitto-tls
- enable tls support in the bundled libmosquitto. This is on by default.vendored-openssl
- build openssl from source, rather than using the system library. Recommended for macOS and Windows users to enable this.
Modules
- router
router
Structs
- A high-level, asynchronous mosquitto MQTT client
- Represents the status of the connection attempt. The embedded status code value depends on the protocol version that was setup for the client. For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html Use the
is_successful
method to test whether the connection was successfully initiated. - Represents the version of the linked mosquitto client library
- Represents a received message that matches one or more of the subscription topic patterns on a client.
Mosq
is the low-level mosquitto client. You probably want to look at Client instead.
Enums
- An event received either from the broker, or from the thread that is managing the connection to the broker.
Traits
- Defines handlers that can be used to determine when various functions have completed. Take care: the underlying mosquitto library can make nested/reentrant calls through your
Callbacks
implementation. If you use interior mutability, be sure to limit the scope/duration of any locks such that they do no encompass any other calls (such as attempts to publish or subscribe) into mosquitto.
Functions
- Returns the version information for the linked mosquitto library
Type Aliases
- Represents an individual message identifier. This is used in this client to determine when a message has been sent.
- An OpenSSL password callback (see
man SSL_CTX_set_default_passwd_cb_userdata
).