PublishAsync does not fail when MQTTnet server is not running
PublishAsync does not fail when MQTTnet server is not running
I am using MQTTNet, a .NET library for MQTT based communication from below link
https://github.com/chkr1011/MQTTnet
I am using managed MQTT Client
from below link
managed MQTT Client
https://github.com/chkr1011/MQTTnet/wiki/ManagedClient
I have below code to publish
messages to MQTT server
.
publish
MQTT server
var messagePayload = new MqttApplicationMessageBuilder()
.WithTopic("Topic1")
.WithPayload(message)
.WithExactlyOnceQoS()
.WithRetainFlag()
.Build();
try
{
await mqttClient.PublishAsync(messagePayload);
}
catch (Exception ex)
{
//Why I am not getting exception?
}
1 Answer
1
This behaviour by design of the MQTT Managed client. It uses an internal queue. All the messages published at added to this queue and are then sent to the server when it is available.
It also ensures that the messages are not lost and can be resent even if the application restarts. Quote from the project wiki.
All MQTT application messages are added to an internal queue and processed once the server is available.
All MQTT application messages can be stored to support sending them after a restart of the application
In this case where successful delivery is guaranteed (Eventual consistency) I'd suggest only returning message acknowledgement to the client. Unless you are worried that the server availability is a frequent and unreliable scenario.
– IUnknown
Jul 2 at 11:03
Another option could be using the ApplicationMessageProcessed event of the ManagedMqttClient class to wait for successful processing to signal the client that the message is processed.
– IUnknown
Jul 2 at 11:12
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
In this case, how do we send message delivery status to the client ? It will be always success! Do I have to query the end system if message reached or not?
– codetoshare
Jul 2 at 10:12