Expand description
MQTT 5 broker for the RustStream messaging framework,
backed by rumqttc: topic filters with wildcards, shared
subscriptions, retained publishes, and headers that ride user properties.
A subscription is a topic or a topic filter the server delivers matching messages to, and a publish is one PUBLISH packet with a quality of service and a retain flag. The crate owns one task that drives the client’s event loop, hands each packet to the subscriptions whose filters match, and reconnects when the connection drops.
§A service
A handler is an async fn over the decoded payload, mounted on a broker inside the application
object, and the attribute writes the main:
use std::time::Duration;
use ruststream_rumqttc::prelude::*;
use serde::Deserialize;
#[derive(Deserialize)]
struct Telemetry {
device: String,
temperature: f64,
}
#[subscriber(MqttFilter::new("devices/+/telemetry").qos(Qos::AtLeastOnce).shared("workers"))]
async fn handle(telemetry: &Telemetry) -> HandlerOutcome {
tracing::info!(telemetry.device, telemetry.temperature, "reading recorded");
HandlerOutcome::ack()
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("telemetry", "0.1.0")).with_broker(
MqttBroker::new("mqtt://localhost:1883", "telemetry-svc")
.keep_alive(Duration::from_secs(30))
.clean_start(false)
.session_expiry(Duration::from_hours(1)),
|b| {
// A filter names no topic a publisher can use, so the mount site says where a
// deferred retry copy goes.
b.include(handle)
.out_retry(Publish::default())
.to("devices/retry/telemetry");
},
)
}cargo run -- run starts it. MqttBroker::new only records the URL and the client id; the
runtime connects the broker at startup and disconnects it when the service stops.
§Where things are
The guides take an MQTT service from installation to production, in this order:
getting started, topics, wildcards and shared subscriptions,
delivery guarantees, device state, publishing commands,
retries and dead letters, request and response,
testing and connecting to production. The
architecture pages show how the crate sits between rumqttc and the runtime, and
the benchmarks what it costs.
Each topic below links to its component page on the site, which shows it in a service; the item’s own documentation keeps the detail.
§Broker
- MqttBroker:
MqttBroker, connected intoConnectedMqttBroker. - Security:
MqttBroker::credentials,MqttBroker::tls_caandMqttBroker::tls_client_auth. - Session:
MqttBroker::clean_startandMqttBroker::session_expiry, a session the server keeps between connections. - last_will:
MqttBroker::last_will, the message the server publishes when the connection ends without a clean disconnect. - Limits:
MqttBroker::max_packet_sizeandMqttBroker::receive_maximum. - Reconnect: the backoff from 100 ms to 5 s, and
MqttBroker::keep_alive. - Startup: the wait for the server’s
CONNACK, and the refusals that fail it. - Shutdown: the clean
DISCONNECT, andMqttError::NotConnectedfor a publisher used afterwards. - Prelude:
prelude, the one glob an MQTT service imports. - Errors:
MqttError.
§Subscriptions
- Subscriptions: a bare topic in
#[subscriber]reads that one topic. - MqttTopic:
MqttTopic, one topic, which answers for its own retry copies. - MqttFilter:
MqttFilter, a topic filter with the+and#wildcards. - Qos:
Qos, the quality of service a subscription reads at. - shared:
MqttFilter::sharedandMqttTopic::shared, one member of a share group takes each message. - Overlaps: overlapping filters on one connection, each receiving a message once.
- Batches: a slice handler over
MqttSubscriberbatches assembled on the client. - Lanes:
workers(n, by_key), one lane perPARTITION_KEY_HEADERvalue, in order.
§Settlement
- Acknowledgement: what an
ackand adropsend on anMqttMessageat each quality of service. - retry: a delivery left unacknowledged until a persistent session resumes.
- retry_after: a copy the runtime publishes after a delay, within the session.
- Retries:
max_attempts, counted on the framework’s retry-count header. - Dead letters:
dead_letter, the topic a spent delivery goes to. - out_retry: where the retry copies of an
MqttFilterregistration go.
§Context
- MqttContext:
MqttContextand its keyDeliveryTopic, the topic a message was published to.
§Publishing
- MqttPublish:
MqttPublish, the publish policy, and its liveMqttPublisher. - MqttPublishSteps:
MqttPublishStepsandMqttPublishOptions, the quality of service and retain flag of one message. - Retained: a message the server keeps on its topic for later subscribers.
- Headers: headers as MQTT 5 user properties, and the three that take the protocol’s own properties.
- Responders: a handler that answers on the response topic of each request.
§Tooling
- MQTT bindings: the server,
operations, messages and
replies of the generated
AsyncAPIdocument (featureasyncapi). - Testing: the in-process MQTT server
TestAppconnects the broker to (featuretesting), and Live runs against a running server.
§Cargo features
Both are off by default and additive. testing adds the in-process mode TestApp::start
connects the broker through, and asyncapi contributes the mqtt bindings to the generated
document. The codecs, the macros and the harness itself are features of the core crate.
Modules§
- prelude
- The imports a routes file on MQTT writes every time, in one glob.
Structs§
- Connected
Mqtt Broker - The typed witness that
connectsucceeded: holds the live client directly, or the in-process transport when the test harness connected it. - Delivery
Topic - The key for
MqttContext::topic. - Mqtt
Broker - An MQTT 5 broker for the
RustStreammessaging framework. - Mqtt
Context - The per-delivery context of this broker.
- Mqtt
Filter - A subscription descriptor for one MQTT topic filter, wildcards included.
- Mqtt
Message - A message delivered by an
MqttSubscriber. - Mqtt
Publish - The publish policy for
MqttPublisher: quality of service and the retain flag as pure declaration, paired with the connected broker by the runtime afterconnect. - Mqtt
Publish Options - The two arguments MQTT carries on every PUBLISH packet, as one message asked for them.
- Mqtt
Publisher - Publishes messages to MQTT topics through the shared connection.
- Mqtt
Subscriber - A subscription to one MQTT topic filter; yields
MqttMessages one at a time, or in batches. - Mqtt
Topic - A subscription descriptor for one MQTT topic.
Enums§
- Mqtt
Error - Errors returned by the MQTT 5 broker.
- Qos
- Delivery quality of service for a subscription or a publish policy.
Constants§
- PARTITION_
KEY_ HEADER - The header that carries a message’s partition key, which keyed worker lanes
(
workers(n, by_key)) order by.
Traits§
- Mqtt
Publish Steps - The per-message steps of an MQTT publish, on the publish builder itself.