Understanding MQTT QOS Levels- Part 2

mqtt-qosIn Part 1 we looked at QOS level 0 and 1. In the second part we look at QOS level 2.

QOS 2 – Only Once

mqtt-publish-flow-qos-2
Click To Enlarge

This level guarantees that the message will be delivered only once.

This is the slowest method as it requires 4 messages.

1- The sender sends a message and waits for an acknowledgement (PUBREC)

2 -The receiver sends a PUBREC message

3. If the sender doesn’t receive an acknowledgement ( PUBREC)  it will resend the message with the DUP flag set.

4. When the sender receives an acknowledgement message PUBREC it then sends a message release message (PUBREL).

5. If the sender doesn’t receive the PUBREL it will resend the PUBREC message

5. When the receiver receives the PUBREL message it can now forward the message onto any subscribers.

6. The receiver then send a publish complete (PUBCOMP) .

7. If the sender doesn’t receive the PUBCOMP message it will resend the PUBREL message.

8. When the sender receives the PUBCOMP the process is complete and it can delete the message from the outbound queue.

QOS 2 Example

Overview

For this example I have created a Python script to publish messages with a QOS of 2.

I have also hacked the Python Client Class so that I can suppress reception of the PUBREC and PUBCOMP messages to simulate a network failure.

In addition I have a monitor subscribed to the topics that I’m publishing on, so I can see all published messages.

The example tries to illustrate:

  • Normal QOS 2 publish and message deletion
  • How Unacknowledged messages are handled
  • Message re-sending on failure
  • Duplicate Flag usage
  • Message storage on Client in case of disconnection

Step 1

To start we do a simple publish with QOS=2 and observe the PUBREC,PUBREL and PUBCOMP messages under normal conditions.

We also observe  the message being marked as delivered, and removed from the outbound message queue.

mqtt-qos-2-normal-pub

Step 2

Then we simulate a network problem by blocking  the PUBREC message.

Now you should notice that the message remains stuck in the outbound message queue.

mqtt-qos-2-normal-pub-2

Step 3

Now we send two more messages this time a QOS 0 message, and a QOS2 message. Notice that the QOS 2 message gets stuck in the outbound queue as it is waiting the acknowledgement handshake.

mqtt-qos-2-normal-pub-3

Step 4

Now we disconnect and reconnect to demonstrate that the client still remembers the messages, and it tries again to re-send the message with the DUP flag set.

mqtt-qos-2-normal-pub-4

Step 5

Now we allow the PUBREC messages through and we see

mqtt-qos-2-normal-pub-5

Step 6

If you examine the monitor output you can see that no duplicate messages have been received.

mqtt-qos-2-monitor


Client to Client QOS

The Quality of service between two clients connected to a broker is determined by the QOS of the published message,and the QOS of the subscribing client.

When a client subscribes to a broker with a QOS it is effectively instructing the broker to publish messages to it, with that QOS.

The overall QOS is always equal to the lowest QOS of the publish or subscribe, as shown in the table below

QOS PUBLISH QOS SUBSCRIBE OVERALL QOS
0 0 or 1 or 2 0
1 0 0
1 1 or 2 1
2 0 0
2 1 1
2 2 2

Related Tutorials and Resources:

Save

Save

Please rate? And use Comments to let me know more
[Total: 3    Average: 5/5]

Leave a Reply

Your email address will not be published. Required fields are marked *