Crate finmqtt_tokio
Expand description
Session-oriented MQTT v5 client built on [tokio] and
finmqtt-core.
§Quick start
use finmqtt_tokio::transport::TcpTransport;
use finmqtt_tokio::{ClientOptions, MqttClient, QoS, Subscription};
use futures::StreamExt;
let transport = TcpTransport::new("broker.example.com", 1883);
let options = ClientOptions::new("my-client".parse()?).reconnect();
let (owner, client, mut messages) = MqttClient::builder(transport, options).start().await?;
// Subscribe: returns SUBACK reason codes.
let reason_codes = client
.subscribe(Subscription::new("sensors/#".parse()?, QoS::AtLeastOnce))
.send()
.await?;
// Publish with v5 properties:
client
.publish(
"sensors/temp".parse()?,
b"22.5".as_slice(),
QoS::AtLeastOnce,
)
.message_expiry(60)
.send()
.await?;
// All messages arrive on the single message stream:
while let Some(msg) = messages.next().await {
println!("{}: {:?}", msg.topic, msg.payload);
}
// The owner handle controls the session lifecycle.
owner.stop().await;§Feature flags
All features are disabled by default:
tls: TLS transport viatokio-rustls(TlsTransport).turso: session persistence as queryable rows in a turso (SQLite-compatible) database (TursoPersistence).tracing: diagnostics via thetracingcrate.
The tls and turso features expose types from tokio-rustls and
turso, which have not reached 1.0 themselves. The API behind these
features therefore tracks those crates’ versions and is exempt from this
crate’s semver stability promise.
Re-exports§
pub use self::builder::ClientBuilder;pub use self::persistence::TursoPersistence;tursopub use self::persistence::FilePersistence;pub use self::persistence::SessionPersistence;
Modules§
- builder
- Builder for configuring and connecting an
MqttClient. - client 🔒
- Session-oriented MQTT v5 client.
- connection 🔒
- Low-level single-connection driver.
- dispatch 🔒
- Shared dispatch machinery for the lazy
publish,subscribe, andunsubscribefutures. - error
- Error types for the MQTT tokio client.
- handle 🔒
- Owner handle for the connection event loop.
- message 🔒
- MQTT v5 message type with full property support.
- options 🔒
- Client options with MQTT v5 connect properties and reconnect configuration.
- persistence
- Session persistence: backend-agnostic configuration and runtime dispatch.
- publish 🔒
- status 🔒
- Connection status monitoring.
- subscribe 🔒
- subscription 🔒
- transport
- Modular transport layer.
Structs§
- Backoff
Multiplier - Exponential backoff multiplier, constrained to be finite and
>= 1.0. - Client
Options - Options for the MQTT v5 client.
- Connection
State Watch - A handle for observing the connection status of an
MqttClient. - Connection
Status Stream - A stream of
ConnectionStatustransitions. - Event
Loop Handle - Owns the connection event loop and controls its lifecycle.
- Jitter
- Backoff jitter as a fraction of the delay, constrained to
0.0..=1.0. - Message
- An incoming MQTT v5 message from the broker.
- Message
Stream - A stream of incoming MQTT messages.
- Mqtt
Bytes - A byte slice that satisfies the MQTT v5 Binary Data length limit (at most 65 535 bytes).
- Mqtt
Bytes Buf - Owned binary data that satisfies the MQTT v5 length limit.
- Mqtt
Client - A session-oriented MQTT v5 client.
- MqttStr
- A UTF-8 string that satisfies all MQTT v5 requirements:
- Mqtt
String - An owned UTF-8 string that satisfies all MQTT v5 requirements.
- NonZero
Vbi - Non-zero Variable Byte Integer.
- Packet
Id - MQTT Packet Identifier (§2.2.1).
- Publish
Builder - Builder for publishing a message with optional MQTT v5 properties.
- Publish
Future - A pending publish operation.
- Publish
Options - MQTT v5 properties to attach to an outgoing PUBLISH.
- Queued
Publish Future - Future returned by
PublishFuture::queued. - Queued
Subscribe Future - A lazy future that resolves as soon as the subscription is recorded locally.
- Queued
Unsubscribe Future - A lazy future that resolves as soon as the unsubscribe is recorded locally.
- Reconnect
Options - Configuration for automatic reconnection with exponential backoff.
- Subscribe
Builder - Builder for subscribing to one or more topic filters.
- Subscribe
Future - A lazy future returned by
SubscribeBuilder::send. - Subscription
- A single topic filter with its associated subscription options.
- Topic
- A validated, borrowed MQTT topic name.
- Topic
Buf - An owned, validated MQTT topic name.
- Topic
Filter - A validated, borrowed MQTT topic filter.
- Topic
Filter Buf - An owned, validated MQTT topic filter.
- Unsubscribe
Builder - Builder for unsubscribing from one or more topic filters.
- Unsubscribe
Future - A lazy future returned by
UnsubscribeBuilder::send. - Will
Options - Configuration for the MQTT will message (Last Will and Testament).
Enums§
- Clean
Start - Whether to request a clean start on the initial connect (§3.1.2.4).
- Connection
Status - The connection status of an
MqttClient. - Disconnect
Cause - Why a connection went down, carried by
Reconnecting. Deliberately coarse; the full error detail is available throughtracing. - Payload
Format - Payload format indicator (§3.3.2.3.2).
- Publish
Progress - Observable lifecycle of a publish operation.
- QoS
- Quality of Service level.
- Reason
Code - All MQTT v5 reason codes.
- Retain
Handling - Retain handling option for SUBSCRIBE.
- Topic
Error - Error returned when constructing a
Topic,TopicFilter, or their owned counterparts.
Constants§
- DEFAULT_
CONNECT_ TIMEOUT - Default
connect_timeout: 30 seconds. - DEFAULT_
MAX_ PACKET_ SIZE - Default
maximum_packet_size: 16 MiB.