Path: redb-route/redb.Route.MqttNet/MqttComponent.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/MqttComponent.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Extensions;
using redb.Route.Core;
using redb.Route.MqttNet.Connection;
namespace redb.Route.MqttNet;
///
/// MQTT component registered with scheme "mqtt". Creates endpoints from URIs.
///
public class MqttComponent : ComponentBase
{
///
public override string Scheme => "mqtt";
/// The broker registry for named broker lookups.
internal IMqttBrokerRegistry? BrokerRegistry { get; set; }
/// The client factory for creating MQTT connections.
internal IMqttClientFactory? ClientFactory { get; set; }
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new MqttEndpointOptions();
options.BindFromUri(uri.RawParameters);
// Resolve the named ConnectionFactory BEFORE Validate(): the factory may be the only
// source of the broker address and credentials, and Validate() requires a broker.
if (!string.IsNullOrEmpty(options.ConnectionFactory))
{
// A set-but-unknown name fails loud -- never a silent fallback to URI params.
var factory = Context.GetRequiredFromRegistry(options.ConnectionFactory);
factory.ApplyTo(options, uri);
}
options.Validate();
return new MqttEndpoint(uri, this, options);
}
}
///
/// MQTT endpoint. Creates consumer (subscriber) or producer (publisher) based on mode.
///
public class MqttEndpoint : EndpointBase
{
/// Creates a new MQTT endpoint.
public MqttEndpoint(EndpointUri uri, MqttComponent component, MqttEndpointOptions options)
: base(uri, component, options) { }
/// The owning MQTT component.
internal MqttComponent MqttComponent => (MqttComponent)Component;
/// Endpoint options.
internal MqttEndpointOptions EndpointOptions => Options;
/// The topic from the URI path.
public string Topic => Uri.Path;
///
public override IProducer CreateProducer()
{
return new MqttProducer(this, Options);
}
///
public override IConsumer CreateConsumer(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
if (Options.Mode != MqttMode.Subscribe)
throw new InvalidOperationException(
$"MQTT consumer requires Mode=Subscribe, but Mode={Options.Mode}.");
return new MqttConsumer(this, processor, Options);
}
}