Path: redb-route/redb.Route.MqttNet/Connection/MqttClientFactory.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/Connection/MqttClientFactory.cs ---- using MQTTnet; namespace redb.Route.MqttNet.Connection; /// /// Creates and manages instances. /// public interface IMqttClientFactory { /// Creates a connected MQTT client from broker options. Task CreateConnectedClientAsync( MqttBrokerOptions options, string? clientIdOverride = null, CancellationToken ct = default); } /// /// Default factory using MQTTnet's . /// internal sealed class DefaultMqttClientFactory : IMqttClientFactory { /// public async Task CreateConnectedClientAsync( MqttBrokerOptions options, string? clientIdOverride = null, CancellationToken ct = default) { var factory = new MqttClientFactory(); var client = factory.CreateMqttClient(); var clientId = clientIdOverride ?? options.ClientId ?? $"redb-route-{Guid.NewGuid():N}"; var builder = new MqttClientOptionsBuilder() .WithTcpServer(options.Server, options.Port) .WithClientId(clientId) .WithKeepAlivePeriod(TimeSpan.FromSeconds(options.KeepAlive)) .WithCleanSession(options.CleanSession); // MQTT 5 (MQTTnet's default protocol): without the interval the session ends with the connection. if (options.SessionExpiryInterval > 0) builder.WithSessionExpiryInterval((uint)options.SessionExpiryInterval); if (options.Username != null) builder.WithCredentials(options.Username, options.Password); if (options.UseTls) builder.WithTlsOptions(o => { }); var mqttOptions = builder.Build(); await client.ConnectAsync(mqttOptions, ct).ConfigureAwait(false); return client; } }