Reliable machine to machine communication using MQTT QoS

MQTT messaging provides three levels of quality of service. Depending upon the requirement QoS levels can be selected for various services.

QoS level 0

This is the default QoS level when QoS is not explicitly set. This is simplest form of MQTT message where client publishes the message to broker and there is no acknowledgment sent by broker. Here you will see only “PUBLISH” on client in transaction.

Here is an example

Publisher:

manish@iotbox:~ $ mosquitto_pub -h 192.168.0.23 -p 1883 -t "/topic/test" -m "message 1" -u iotuser -P iotxxx -q 0 -d
Client mosqpub/30781-iotbox.mk sending CONNECT
Client mosqpub/30781-iotbox.mk received CONNACK
Client mosqpub/30781-iotbox.mk sending PUBLISH (d0, q0, r0, m1, '/topic/test', … (9 bytes))
Client mosqpub/30781-iotbox.mk sending DISCONNECT

Subscriber:

manish@homebox:~ $ mosquitto_sub -h 192.168.0.23 -p 1883 -t "/topic/test" -u iotuser -P iotxxx -q 0 -d
Client mosqsub/21752-homebox sending CONNECT
Client mosqsub/21752-homebox received CONNACK
Client mosqsub/21752-homebox sending SUBSCRIBE (Mid: 1, Topic: /topic/test, QoS: 0)
Client mosqsub/21752-homebox received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/21752-homebox received PUBLISH (d0, q0, r0, m0, '/topic/test', … (9 bytes))
message 1

QoS Level 1

This QoS level ensures that message gets delivered at least once to the broker. Client expects an acknowledgement from broker and if acknowledgement is not received then client keeps sending message until it receives acknowledgment. In this case client subscribers can sometimes receive duplicate messages.
Here you will see “PUBLISH” and “PUBACK” on client in transaction.

Here is an example of QoS Level 1 message transaction

Publisher:

manish@iotbox:~ $ mosquitto_pub -h 192.168.0.23 -p 1883 -t "/topic/test" -m "message 1" -u iotuser -P iotxxx -q 1 -d
Client mosqpub/30982-iotbox.mk sending CONNECT
Client mosqpub/30982-iotbox.mk received CONNACK
Client mosqpub/30982-iotbox.mk sending PUBLISH (d0, q1, r0, m1, '/topic/test', … (9 bytes))
Client mosqpub/30982-iotbox.mk received PUBACK (Mid: 1)
Client mosqpub/30982-iotbox.mk sending DISCONNECT

Subscriber:

manish@homebox:~ $ mosquitto_sub -h 192.168.0.23 -p 1883 -t "/topic/test" -u iotuser -P iotxxx -q 1 -d
Client mosqsub/21885-homebox sending CONNECT
Client mosqsub/21885-homebox received CONNACK
Client mosqsub/21885-homebox sending SUBSCRIBE (Mid: 1, Topic: /topic/test, QoS: 1)
Client mosqsub/21885-homebox received SUBACK
Subscribed (mid: 1): 1
Client mosqsub/21885-homebox received PUBLISH (d0, q1, r0, m1, '/topic/test', … (9 bytes))
Client mosqsub/21885-homebox sending PUBACK (Mid: 1)
message 1

QoS Level 2

This is most reliable level where client and broker ensure that message from client is published exactly once to the broker. To achieve this four messages are used between client and broker to complete handshake and ensure message is delivered from client to broker.
Here you will see “PUBLISH”, “PUBREC”, “PUBREL” and “PUBCOMP” in transaction.

Here is an example.

Publisher:

manish@iotbox:~ $ mosquitto_pub -h 192.168.0.23 -p 1883 -t "/topic/test" -m "message 1" -u iotuser -P iotxxx -q 2 -d
Client mosqpub/31164-iotbox.mk sending CONNECT
Client mosqpub/31164-iotbox.mk received CONNACK
Client mosqpub/31164-iotbox.mk sending PUBLISH (d0, q2, r0, m1, '/topic/test', … (9 bytes))
Client mosqpub/31164-iotbox.mk received PUBREC (Mid: 1)
Client mosqpub/31164-iotbox.mk sending PUBREL (Mid: 1)
Client mosqpub/31164-iotbox.mk received PUBCOMP (Mid: 1)
Client mosqpub/31164-iotbox.mk sending DISCONNECT

Subscriber:

manish@homebox:~ $ mosquitto_sub -h 192.168.0.23 -p 1883 -t "/topic/test" -u iotuser -P iotxxx -q 2 -d
Client mosqsub/22184-homebox sending CONNECT
Client mosqsub/22184-homebox received CONNACK
Client mosqsub/22184-homebox sending SUBSCRIBE (Mid: 1, Topic: /topic/test, QoS: 2)
Client mosqsub/22184-homebox received SUBACK
Subscribed (mid: 1): 2
Client mosqsub/22184-homebox received PUBLISH (d0, q2, r0, m1, '/topic/test', … (9 bytes))
Client mosqsub/22184-homebox sending PUBREC (Mid: 1)
Client mosqsub/22184-homebox received PUBREL (Mid: 1)
message 1
Client mosqsub/22184-homebox sending PUBCOMP (Mid: 1)