ManagedClient

morganics edited this page Oct 24, 2018 · 4 revisions

Preparation

This project also contains a managed MQTT client. The client has some additional functionality in comparison with the regular MqttClient. Those functionalities are reflecting the most common use cases and thus the ManagedClient provides a out-of-the-box MQTT client with the following features.

  1. The managed client is started once and will maintain the connection automatically including reconnecting etc.
  2. All MQTT application messages are added to an internal queue and processed once the server is available.
  3. All MQTT application messages can be stored to support sending them after a restart of the application
  4. All subscriptions are managed across server connections. There is no need to subscribe manually after the connection with the server is lost.

The managed Client is available as a separate nuget package (https://www.nuget.org/packages/MQTTnet.Extensions.ManagedClient/).

The following code shows how to setup and start a managed MQTT client.

// Setup and start a managed MQTT client.
var options = new ManagedMqttClientOptionsBuilder()
    .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
    .WithClientOptions(new MqttClientOptionsBuilder()
        .WithClientId("Client1")
        .WithTcpServer("broker.hivemq.com")
        .WithTls().Build())
    .Build();

var mqttClient = new MqttFactory().CreateManagedMqttClient();
await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("my/topic").Build());
await mqttClient.StartAsync(options);

// StartAsync returns immediately, as it starts a new thread using Task.Run, 
// and so the calling thread needs to wait.
Console.ReadLine();

Message processing

Similar to the regular client the managed client also has a Publish method. But for the managed client this method will not throw an exception on failure because the message is only added to an internal queue and processed later.

But the managed client has several events which allows dealing with send errors etc.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.