Skip to main content

Module tokio

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 via tokio-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, and unsubscribe futures.
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§

BackoffMultiplier
Exponential backoff multiplier, constrained to be finite and >= 1.0.
ClientBuilder
Builder for an MqttClient.
ClientOptions
Options for the MQTT v5 client.
ConnectionStateWatch
A handle for observing the connection status of an MqttClient.
ConnectionStatusStream
A stream of ConnectionStatus transitions.
EventLoopHandle
Owns the connection event loop and controls its lifecycle.
FilePersistence
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.
MessageStream
A stream of incoming MQTT messages.
MqttClient
A session-oriented MQTT v5 client.
PublishBuilder
Builder for publishing a message with optional MQTT v5 properties.
PublishFuture
A pending publish operation.
PublishOptions
MQTT v5 properties to attach to an outgoing PUBLISH.
QueuedPublishFuture
Future returned by PublishFuture::queued.
QueuedSubscribeFuture
A lazy future that resolves as soon as the subscription is recorded locally.
QueuedUnsubscribeFuture
A lazy future that resolves as soon as the unsubscribe is recorded locally.
ReconnectOptions
Configuration for automatic reconnection with exponential backoff.
SessionPersistence
Configures how a client’s session is durably persisted across restarts.
SubscribeBuilder
Builder for subscribing to one or more topic filters.
SubscribeFuture
A lazy future returned by SubscribeBuilder::send.
Subscription
A topic filter with its associated subscription options.
TcpTransport
Plain TCP transport.
TlsTransporttls
TLS transport using tokio-rustls.
TursoPersistenceturso
Configures session persistence backed by a turso database.
UnsubscribeBuilder
Builder for unsubscribing from one or more topic filters.
UnsubscribeFuture
A lazy future returned by UnsubscribeBuilder::send.
WillOptions
Configuration for the MQTT will message (Last Will and Testament).

Enums§

CleanStart
Whether to start a new session when the client starts (§3.1.2.4).
ConnectionStatus
The connection status of an MqttClient.
DisconnectCause
Why a connection went down, reported by Reconnecting. Deliberately coarse. The full error detail is available through tracing.
Error
Errors that can occur in the MQTT client.
FilterOutcome
The result for one topic filter of a subscribe or unsubscribe request.
PublishProgress
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.