Path: redb-route/redb.Route.MqttNet/README.md Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.MqttNet/README.md ---- # redb.Route.MqttNet MQTT 5.0 transport for redb.Route via MQTTnet. Subscribe consumer, publish producer, shared subscriptions, QoS levels, retained messages, and TLS. [![NuGet](https://img.shields.io/nuget/v/redb.Route.MqttNet?label=NuGet&color=blue)](https://www.nuget.org/packages/redb.Route.MqttNet) [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue)](../../LICENSE) ## Installation ```bash dotnet add package redb.Route.MqttNet ``` ## Usage ### Fluent DSL ```csharp using redb.Route.MqttNet; // Subscribe to topic From(Mqtt.Subscribe("sensors/temperature/#") .Server("mqtt.example.com").Port(1883) .ClientId("route-consumer") .Qos(1)) .Log("Temperature: ${body}") .To("direct://process"); // Publish to topic From("direct://alerts") .To(Mqtt.Publish("alerts/critical") .Server("mqtt.example.com") .Username("svc").Password("secret") .Qos(2) .Retain()); // Shared subscription (load balancing across consumers) From(Mqtt.Subscribe("orders/new") .Server("broker.local") .SharedSubscription("order-processors") .UseTls()) .To("direct://handle-order"); // Broker configuration (shared across routes) From(Mqtt.Subscribe("events/#") .Broker("main") .Qos(1)) .To("seda://events"); ``` ## Fluent Builder API | Category | Methods | |----------|---------| | **Connection** | `.Broker(name)`, `.Server()`, `.Port()`, `.Username()`, `.Password()`, `.ClientId()`, `.UseTls()`, `.KeepAlive()`, `.CleanSession()`, `.SessionExpiryInterval()`, `.MaxReconnectDelay()` | | **QoS** | `.Qos(level)` — AtMostOnce, AtLeastOnce, ExactlyOnce | | **Subscribe** | `Mqtt.Subscribe(topic)`, `.SharedSubscription(group)`, `.ConcurrentConsumers(n)` | | **Publish** | `Mqtt.Publish(topic)`, `.Retain()`, `.MessageExpiryInterval()`, `.ContentType()`, `.ResponseTopic()` | > Most builder methods accept both constant values and `IExpression` for runtime resolution via the expression engine. ## Headers | Header | Written by | Read by the publish | |--------|------------|---------------------| | `redbMqtt.topic` | the consumer (received topic), the producer (topic it published to) | no | | `redbMqtt.overrideTopic` | the route only | yes: the topic instead of the endpoint's | | `redbMqtt.qos`, `redbMqtt.retain` | the consumer (received message), the producer (its publish) | no | | `redbMqtt.overrideQos` (0, 1 or 2), `redbMqtt.overrideRetain` (true or false), typed or as text | the route only | yes: QoS and retain flag instead of the endpoint's | | `redbMqtt.contentType`, `redbMqtt.responseTopic`, `redbMqtt.correlationData`, `redbMqtt.messageExpiryInterval` | the consumer | yes, passed through: the MQTT 5 properties of the publish | | `redbMqtt.userProperties` | the consumer | yes, passed through: MQTT 5 user properties | | `redbMqtt.broker`, `redbMqtt.clientId` | the consumer, the producer | no | The topic a publish goes to is the endpoint's (an expression such as `sensors/${header.room}` included), or `redbMqtt.overrideTopic` when the route set it. `redbMqtt.topic` only tells where the last message came from or went, so two publishes in one route each reach their own endpoint, and a received message republished to a fixed topic goes there, not back to the topic it came from. QoS and the retain flag work the same way: the endpoint's (`qos`, `qosExpression`, `retain`), or `redbMqtt.overrideQos` / `redbMqtt.overrideRetain` when the route set them. A retained message received and republished is not retained again, and goes out with the endpoint's QoS, not the QoS it arrived with. The MQTT 5 properties are the exception: a message received and republished keeps its content type, response topic, correlation data, expiry interval and user properties, so request/response and metadata survive a hop. A route that does not want them on the publish removes those headers first. ### Concurrent processing `.ConcurrentConsumers(n)` (default `1` = serial) processes up to `n` received messages in parallel on a worker pool. Each message is settled **only after** its worker finishes, as in serial mode (see [Acknowledgement and failed messages](#acknowledgement-and-failed-messages)). Ordering is not preserved when `n > 1`. ```csharp From(Mqtt.Subscribe("telemetry/#").Broker("main").Qos(1).ConcurrentConsumers(5)) .Process(IngestReading); ``` See the framework-wide **Concurrency & Parallelism** guide (`CONCURRENCY.md` in the [redb.Route repository](https://github.com/redbase-app/redb)) for how this compares to the `.Threads(N)` processing EIP. ## Acknowledgement and failed messages The client speaks **MQTT 5.0** (MQTTnet's default protocol). The consumer acknowledges every QoS 1 and QoS 2 message itself, **after** the route, in serial and concurrent mode alike; MQTTnet does not acknowledge on receipt. MQTT has no negative acknowledgement: the broker sends an unacknowledged message again only when the client connects again to a session the broker kept, and it sends a client no more unacknowledged messages than its receive window. So a message the route fails on (an exception it did not handle, or `.RollbackAll()`) is settled by one rule, the one of Apache Camel's paho-mqtt5 consumer as corrected by CAMEL-25793: | The failed message | What the consumer does | |---|---| | QoS 0, or the session ends with the connection (`cleanSession=true`, the default, or `sessionExpiryInterval=0`) | **Acknowledges it and logs an error.** | | QoS 1 or QoS 2 on a kept session (`cleanSession=false` and `sessionExpiryInterval` above 0) | Leaves it unacknowledged, disconnects and connects again after a pause, so that the broker sends it again. | What this means for a route: - **Acknowledged and logged** means the broker will not send the message again: it is gone from MQTT. Holding the acknowledgement back would not bring it back either, it would only fill the receive window until the consumer stops getting messages. Where a failed message must be kept, the route keeps it with its own error handling: an error handler with redelivery, and a dead letter channel that stores it. - On a kept session the message comes again after the reconnect and runs through the route again; so do the other messages that were not acknowledged yet when the consumer disconnected, also ones whose route had already finished. A route on a kept session must therefore tolerate a repeat, for example with an idempotent consumer. - The pause before the reconnect is 1 second and doubles with each failed message in a row, up to `maxReconnectDelay` (default 128000 ms); a message that succeeds resets it. A message that always fails is therefore tried at that pace, not in a tight loop. A failure while the consumer is stopping starts no reconnect and is not acknowledged. - A kept session needs a fixed `clientId`: the broker finds the session by it. The consumer does not unsubscribe on stop when the session is kept, so the messages that wait in it reach the next start. ``` mqtt:orders/#?mode=Subscribe&broker=main&qos=1&clientId=orders-in&cleanSession=false&sessionExpiryInterval=3600 ``` ## A lost connection When the broker or the network drops the consumer's connection (a broker restart, a server-side disconnect, a reset), the consumer connects again and subscribes again when the broker did not keep the session; a kept session keeps its subscription, and the broker sends the messages that were not acknowledged on the lost connection again. While the connection is away the endpoint's health is `Warning`, then `Critical` once an attempt to connect failed; a Warning is logged when it is lost and an Information when it is back, and `Reconnects` counts each return. Attempts follow `RedbRoute:Reconnect` (delay, multiplier, cap, attempt timeout); a loss is not counted in `Errors`. The reconnect after a failed message (see above) that cannot reach the broker becomes such a loss. A disconnect the consumer makes itself, on stop or for a failed message, is not one. ## Part of [redb.Route](../README.md) — ESB & EIP Framework for .NET ## Named connection factory Keep credentials out of the route URI: register a factory in the context registry and reference it by name. A set-but-unknown name fails loud at startup — a typo can never silently fall back to inline URI parameters. ```csharp context.AddToRegistry("prod", new MqttConnectionFactory { Server = "mqtt.internal", Username = "svc", Password = secrets.MqttPassword, }); // mqtt://sensors/+?connectionFactory=prod ``` ## Concurrency The default is **1** concurrent consumer — the industry norm (Camel, Spring, the Azure SDK all ship 1): a single consumer preserves ordering and your handlers need no thread safety. Parallelism is an explicit opt-in: ``` concurrentConsumers=4 # a fixed worker count concurrentConsumers=auto # max(CPU count, 2) — the NServiceBus formula ``` Anything else — `0`, a negative, a typo — fails at endpoint creation naming the option (the old int-typed option silently fell back to 1). Raising the value trades ordering for throughput: messages from the same queue are processed out of order, and your processors must be safe to run in parallel. ## Tracing On the `redb.Route` activity source (`AddSource("redb.Route")`), with `messaging.system` = `mqtt` and `redb.route.endpoint`: - **Context.** The W3C context (`traceparent`, `tracestate`, `baggage`) travels in MQTT 5 user properties, and only when the connection speaks v5 (the MQTTnet default). Under 3.1.1 nothing is written — MQTTnet refuses user properties there — and nothing is read. - **Producer.** One `Producer` span per publish, `{topic} publish`, whose context is written to the message. The write replaces a `traceparent` already in `redbMqtt.userProperties` (copied from an earlier hop); the exchange's header is left as it was. An error when the publish fails, unless our own token cancelled it. - **Consumer.** One `Consumer` span per routed message, `{topic} receive`, a child of the sender's context when the message carries one; otherwise a root, never a child of the activity the client's receive loop holds. Nothing arriving opens none. A failed route marks it an error; our own stop does not. - `RouteEngineOptions.EnableTelemetry = false` opens neither span; a context the route received is still passed on.