Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When publishing message with mqtt QoS 2, can it be lost?

I am trying to implement an MQTT client, using MQTT-Client Framework. I want to assure that every message I try to publish reaches the broker. I can't figure out exactly what QOS2 means: It states that a message will be sent exactly once. Does it mean that when connection is lost, it will try to retransmit the message automatically after reconnecting? Or this should be handled by the app?

Also in this library, reconnection is done by default automatically? Or there is need to check if connectionLost happens and then try to reconnect?

like image 437
angelos_lex Avatar asked Nov 28 '18 22:11

angelos_lex


1 Answers

MQTT QoS levels are guarantees on the delivery of messages to the receiver - not on how often a message is sent/resent by the sender. See the QoS section in the MQTT spec and an overview of MQTT QoS.

A message published with MQTT QoS2 means that it will be delivered exactly once. The message could be sent more than once to achieve this exactly once delivery guarantee.

The at least once delivery aspect for MQTT is achieved using the PUBLISH/PUBREC handshake. The publisher will continue to resend a PUBLISH message with the DUP flag set if it does not receive a PUBREC packet that acknowledges its published message(s).

The exactly once delivery aspect for QoS2 is achieved using an addition PUBREL/PUBCOMP handshake. The receiver can choose to forward the message and discard duplicate messages at two different points.

Does it mean that when connection is lost, it will try to retransmit the message automatically after reconnecting? Or this should be handled by the app?

The MQTT spec covers message delivery retries:

When a Client reconnects with CleanSession set to 0, both the Client and Server MUST re-send any unacknowledged PUBLISH Packets (where QoS > 0) and PUBREL Packets using their original Packet Identifiers. This is the only circumstance where a Client or Server is REQUIRED to redeliver messages.

So if your client follows the spec and you are using a persistent session (CleanSession = 0) then messages will be retransmitted.

like image 150
Ben T Avatar answered Jan 01 '23 08:01

Ben T