In this code sample, a message is published to the server at QoS 2. This means it is assured to be delivered once and only once.
In order to achieve this, the message needs to be persisted by the client. A default implementation of persistence is provided by MqttDefaultFilePersistence
. The client uses this unless you supply an alternative.
// Create a client to communicate with a broker at the specified address
MqttClient client = new MqttClient("tcp://localhost:1883", "MyClientID");
// Create message and set quality of service to "2" (assured delivery)
MqttMessage message = new MqttMessage("Hello world");
// Publish the message to the broker
MqttTopic topic = client.GetTopic("foo/bar");
IMqttDeliveryToken token = topic.Publish(message);
// Block until the message is safely with the broker
Parent topic: Using the MQTT .NET Client: XPD621