In this code sample, a message is published to the server at QoS 2. This approach means that it is ensured to be delivered once and only once.
To achieve this delivery, the message needs to be persisted by the client. A default implementation of persistence is provided by MqttDefaultFilePersistence
. The client uses this implementation 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: XPD622