Path: redb-route/redb.Route.MqttNet/MqttConnectionFactory.cs
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/MqttConnectionFactory.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.MqttNet;
///
/// Named connection factory for MQTT. Register it in the route registry and reference it by name
/// from the endpoint URI (connectionFactory=my-broker) so broker credentials never have to
/// appear in the URI, and therefore can never reach logs, telemetry, or the Tsak dashboard.
///
/// Example:
///
/// context.AddToRegistry("iot-broker", new MqttConnectionFactory
/// {
/// Server = "mqtt.corp.local",
/// Port = 8883,
/// UseTls = true,
/// Username = "svc-ingest",
/// Password = Environment.GetEnvironmentVariable("MQTT_PASSWORD")
/// });
///
/// // route carries no credentials at all:
/// r.From("mqtt://sensors/+/temp?connectionFactory=iot-broker")
///
///
///
public sealed class MqttConnectionFactory
{
/// Broker address in host:port form (alternative to ).
public string? Broker { get; set; }
/// Broker hostname or IP.
public string? Server { get; set; }
/// Broker port (1883 plain, 8883 TLS).
public int Port { get; set; }
/// Username for broker authentication.
public string? Username { get; set; }
/// Password for broker authentication.
[Sensitive]
public string? Password { get; set; }
/// MQTT client identifier.
public string? ClientId { get; set; }
/// Connect over TLS.
public bool UseTls { get; set; }
/// Keep-alive interval in seconds (default 60).
public int KeepAlive { get; set; } = 60;
/// Start a clean session (default true).
public bool CleanSession { get; set; } = true;
/// MQTT 5 Session Expiry Interval in seconds (default 0: the session ends with the connection).
public int SessionExpiryInterval { get; set; }
///
/// Copies this factory's connection and credential settings onto the endpoint options, but only
/// for parameters the endpoint URI did not set explicitly — an inline URI value always wins,
/// so existing routes keep their behaviour.
///
internal void ApplyTo(MqttEndpointOptions options, EndpointUri uri)
{
var supplied = uri.RawParameters;
if (!supplied.ContainsKey(nameof(options.Broker))) options.Broker = Broker;
if (!supplied.ContainsKey(nameof(options.Server))) options.Server = Server;
if (!supplied.ContainsKey(nameof(options.Port))) options.Port = Port;
if (!supplied.ContainsKey(nameof(options.Username))) options.Username = Username;
if (!supplied.ContainsKey(nameof(options.Password))) options.Password = Password;
if (!supplied.ContainsKey(nameof(options.ClientId))) options.ClientId = ClientId;
if (!supplied.ContainsKey(nameof(options.UseTls))) options.UseTls = UseTls;
if (!supplied.ContainsKey(nameof(options.KeepAlive))) options.KeepAlive = KeepAlive;
if (!supplied.ContainsKey(nameof(options.CleanSession))) options.CleanSession = CleanSession;
if (!supplied.ContainsKey(nameof(options.SessionExpiryInterval))) options.SessionExpiryInterval = SessionExpiryInterval;
}
}