Module tokio
Available on crate feature
tokio only.Expand description
Session-oriented MQTT v5 client built on [tokio].
§Quick start
use finmqtt_client::tokio::{ClientOptions, MqttClient, Subscription, TcpTransport};
use finmqtt_core::wire::QoS;
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 the outcome of each filter.
let outcomes = 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
The tokio feature enables this module. These features extend it and
imply tokio:
tls: TLS transport viatokio-rustls(TlsTransport).turso: session persistence as queryable rows in a turso (SQLite-compatible) database (TursoPersistence).
Diagnostics are emitted through the tracing crate.
The tls and turso features expose types from tokio-rustls and
turso, which have not reached 1.0 themselves. The API behind these
features follows those crates’ versions and is exempt from this
crate’s semver stability promise.
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.
- filter_
outcome 🔒 - The result for one filter of a SUBSCRIBE or UNSUBSCRIBE.
- 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
Builder - Builder for an
MqttClient. - 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.
- File
Persistence - Configures session persistence backed by a CRC-framed journal file.
- 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
Client - A session-oriented MQTT v5 client.
- 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.
- Session
Persistence - Configures how a client’s session is durably persisted across restarts.
- Subscribe
Builder - Builder for subscribing to one or more topic filters.
- Subscribe
Future - A lazy future returned by
SubscribeBuilder::send. - Subscription
- A topic filter with its associated subscription options.
- TcpTransport
- Plain TCP transport.
- TlsTransport
tls - TLS transport using
tokio-rustls. - Turso
Persistence turso - Configures session persistence backed by a turso database.
- 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 start a new session when the client starts (§3.1.2.4).
- Connection
Status - The connection status of an
MqttClient. - Disconnect
Cause - Why a connection went down, reported by
Reconnecting. Deliberately coarse. The full error detail is available throughtracing. - Error
- Errors that can occur in the MQTT client.
- Filter
Outcome - The result for one topic filter of a subscribe or unsubscribe request.
- Publish
Progress - Observable lifecycle of a publish operation.
Constants§
- DEFAULT_
ACK_ TIMEOUT - Default
ack_timeout: 60 seconds. - DEFAULT_
COMMAND_ CHANNEL_ CAPACITY - Default capacity of the command channel between client handles and the connection event loop.
- DEFAULT_
CONNECT_ TIMEOUT - Default
connect_timeout: 30 seconds. - DEFAULT_
MAX_ PACKET_ SIZE - Default
maximum_packet_size: 16 MiB. - DEFAULT_
MESSAGE_ CHANNEL_ CAPACITY - Default capacity of the inbound message channel that feeds the
MessageStream.
Traits§
- Transport
- A transport that can establish a byte-stream connection to an MQTT broker.