MQTT Encoding and Decoding: Difference between revisions

From IoT with AME
Jump to navigation Jump to search
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 3: Line 3:
=MQTT Encoding and Decoding=
=MQTT Encoding and Decoding=


The MQTT protocol is very lightweight (does not use many bytes to get the message across).
The MQTT protocol is very lightweight. It does not use many bytes to get the message across, and the handshake (sequence of messages) is pretty simple (with user-choosable options ranging from "primitive" to "modestly sophisticated").


Each MQTT packet consists of:
Each MQTT packet consists of:


* Fixed header
* Fixed header
** b7-4: MQTT Packet Typ, b3-0: Flags
** packet type:byte, consisting of the actual MQTT packet type in the higher nibble and some flags in the lower nibble
** Remaining length - this one is a bit tricky and consumes between 1 and 4 bytes. Encoding is least significant bits first in b6-0 while b7 is a "continuation bit" ('1' meaning "another byte follows").
*** b7-4: MQTT packet type
*** b3: DUP flag (PUBLISH only)
*** b2-1: QoS (PUBLISH only)
*** b0: RETAIN flag (PUBLISH only)
** remaining length - this one is a bit tricky and consumes between 1 and 4 bytes. Encoding is "least significant bits first" in b6-0 while b7 is a "continuation bit" ('1' meaning "another byte follows"). This mechanism ensures that short messages stay short by using the fewest number of bytes needed to encode the length.
* Variable header (optional), depending on the package type
* Variable header (optional), depending on the package type
* Payload (optional), depending on the package type
* Payload (optional), depending on the package type


Text is encoded in UTF-8 without a terminating "0" byte, after the number of bytes in the text encoded as two bytes, MSB first.
Type "Array" is binary data, after the number of bytes in the array encoded as two bytes, MSB first.
 
Type "String" is like "Array", with the text encoded in UTF-8 without a terminating "0" byte.


{| class="wikitable" style="border: 2px solid"
{| class="wikitable" style="border: 2px solid"
! rowspan="2" | Type
! rowspan="2" | Packet Type
! rowspan="2" | Number
! rowspan="2" | Direction
! rowspan="2" | Direction
! rowspan="2" | Flags
! rowspan="1" colspan="2" | Fixed Header
! rowspan="1" colspan="2" | Variable Header
! rowspan="1" colspan="2" | Variable Header
! rowspan="2" | Payload
! rowspan="2" | Payload
|-
|-
! Msg ID
! Packet Type
!  
! Flags
! Msg ID?
! Content
|-
|-
| CONNECT
| CONNECT
| Device to Broker
| 1
| 1
| Device to Broker
| 0
| 0
| -
| -
| "MQTT":String<br/>protocol level (4):byte<br/>connect flags:byte (b7: user, b6: password, b5: will retain, b4-3: will QoS, b2: will, b1: clean session)<br/>keep alive:word
|  
| client ID:String<br/>will topic:String (if present)<br/>will payload:Array (if present)<br/>user name:String (if present)<br/>password:Array (if present)
* protocol name:String (always "MQTT")
* protocol level:byte (always 4)
* connect flags:byte
** b7: user
** b6: password, b5: will retain
** b4-3: will QoS
** b2: will
** b1: clean session
* keep alive:word
|
* client ID:String
* will topic:String (if present)
* will payload:Array (if present)
* user name:String (if present)
* password:Array (if present)
|-
|-
| CONNACK
| CONNACK
| Broker to Device
| 2
| 2
| Broker to Device
| 0
| 0
| -
| -
| connect acknowledge flags:byte (b0: session present)<br/>connect return code:byte (0: connection accepted, 1: unacceptable  
|  
protocol version, 2: identifier rejected, 3: server unavailable, 4: bad user name or password, 5: not authorized)
* connect acknowledge flags:byte
** b0: session present
* connect return code:byte
** 0: connection accepted (success)
** 1: unacceptable protocol version (fail)
** 2: identifier rejected (fail)
** 3: server unavailable (fail)
** 4: bad user name or password (fail)
** 5: not authorized (fail)
| -
| -
|-
|-
| PUBLISH
| PUBLISH
| Both
| 3
| 3
| Both
|  
| b3: DUP, b2-1: QoS, b0: RETAIN
* b3: DUP
* b2-1: QoS
* b0: RETAIN
| QoS > 0 ? y
| QoS > 0 ? y
| topic name:String<br/>message ID:word
|
* topic name:String
* message ID:word (if QoS > 0)
| payload:Array (optional)
| payload:Array (optional)
|-
|-
| PUBACK
| PUBACK
| Both
| 4
| 4
| Both
| 0
| 0
| y
| y
| message ID of PUBLISH packet:word
|
* message ID of PUBLISH packet:word
| -
| -
|-
|-
| PUBREC
| PUBREC
| Both
| 5
| 5
| Both
| 0
| 0
| y
| y
| message ID of PUBLISH packet:word
|  
* message ID of PUBLISH packet:word
| -
| -
|-
|-
| PUBREL
| PUBREL
| Both
| 6
| 6
| Both
| 1 (you may read this as "fixed QoS of 1")
| 1
| y
| y
| message ID of PUBREC packet:word
|  
* message ID of PUBREC packet:word
| -
| -
|-
|-
| PUBCOMP
| PUBCOMP
| Both
| 7
| 7
| Both
| 0
| 0
| y
| y
| message ID of PUBREL packet:word
|  
* message ID of PUBREL packet:word
| -
| -
|-
|-
| SUBSCRIBE
| SUBSCRIBE
| Device to Broker
| 8
| 8
| Device to Broker
| 1 (you may read this as "fixed QoS of 1")
| 1
| y
| y
| message ID:word
|  
| topic filter:String<br/>requested QoS:byte (b1-0: QoS)<br/>(repeat sequence one or more times)
* message ID:word
|  
* topic filter:String
* requested QoS:byte (b1-0: QoS)
repeat sequence one or more times
|-
|-
| SUBACK
| SUBACK
| Broker to Device
| 9
| 9
| Broker to Device
| 0
| 0
| y
| y
| message ID of SUBSCRIBE packet:word
|  
| return code:byte (b7: failure, b1-0: maximum QoS)<br/>(repeat sequence one or more times)
* message ID of SUBSCRIBE packet:word
|  
* return code:byte
** b7: failure
** b1-0: maximum QoS
repeat sequence one or more times
|-
|-
| UNSUBSCRIBE
| UNSUBSCRIBE
| Device to Broker
| 10
| 10
| Device to Broker
| 1 (you may read this as "fixed QoS of 1")
| 1
| y
| y
| message ID:word
|  
| topic filter:String<br/>(repeat sequence one or more times)
* message ID:word
|
* topic filter:String
repeat sequence one or more times
|-
|-
| UNSUBACK
| UNSUBACK
| Broker to Device
| 11
| 11
| Broker to Device
| 0
| 0
| y
| y
| message ID of SUBSCRIBE packet:word
|  
* message ID of SUBSCRIBE packet:word
| -
| -
|-
|-
| PINGREQ
| PINGREQ
| Device to Broker
| 12
| 12
| Device to Broker
| 0
| 0
| -
| -
Line 124: Line 175:
|-
|-
| PINGRESP
| PINGRESP
| Broker to Device
| 13
| 13
| Broker to Device
| 0
| 0
| -
| -
Line 132: Line 183:
|-
|-
| DISCONNECT
| DISCONNECT
| Device to Broker
| 14
| 14
| Device to Broker
| 0
| 0
| -
| -

Latest revision as of 12:49, 13 June 2018

MQTT

MQTT Encoding and Decoding

The MQTT protocol is very lightweight. It does not use many bytes to get the message across, and the handshake (sequence of messages) is pretty simple (with user-choosable options ranging from "primitive" to "modestly sophisticated").

Each MQTT packet consists of:

  • Fixed header
    • packet type:byte, consisting of the actual MQTT packet type in the higher nibble and some flags in the lower nibble
      • b7-4: MQTT packet type
      • b3: DUP flag (PUBLISH only)
      • b2-1: QoS (PUBLISH only)
      • b0: RETAIN flag (PUBLISH only)
    • remaining length - this one is a bit tricky and consumes between 1 and 4 bytes. Encoding is "least significant bits first" in b6-0 while b7 is a "continuation bit" ('1' meaning "another byte follows"). This mechanism ensures that short messages stay short by using the fewest number of bytes needed to encode the length.
  • Variable header (optional), depending on the package type
  • Payload (optional), depending on the package type

Type "Array" is binary data, after the number of bytes in the array encoded as two bytes, MSB first.

Type "String" is like "Array", with the text encoded in UTF-8 without a terminating "0" byte.

Packet Type Direction Fixed Header Variable Header Payload
Packet Type Flags Msg ID? Content
CONNECT Device to Broker 1 0 -
  • protocol name:String (always "MQTT")
  • protocol level:byte (always 4)
  • connect flags:byte
    • b7: user
    • b6: password, b5: will retain
    • b4-3: will QoS
    • b2: will
    • b1: clean session
  • keep alive:word
  • client ID:String
  • will topic:String (if present)
  • will payload:Array (if present)
  • user name:String (if present)
  • password:Array (if present)
CONNACK Broker to Device 2 0 -
  • connect acknowledge flags:byte
    • b0: session present
  • connect return code:byte
    • 0: connection accepted (success)
    • 1: unacceptable protocol version (fail)
    • 2: identifier rejected (fail)
    • 3: server unavailable (fail)
    • 4: bad user name or password (fail)
    • 5: not authorized (fail)
-
PUBLISH Both 3
  • b3: DUP
  • b2-1: QoS
  • b0: RETAIN
QoS > 0 ? y
  • topic name:String
  • message ID:word (if QoS > 0)
payload:Array (optional)
PUBACK Both 4 0 y
  • message ID of PUBLISH packet:word
-
PUBREC Both 5 0 y
  • message ID of PUBLISH packet:word
-
PUBREL Both 6 1 (you may read this as "fixed QoS of 1") y
  • message ID of PUBREC packet:word
-
PUBCOMP Both 7 0 y
  • message ID of PUBREL packet:word
-
SUBSCRIBE Device to Broker 8 1 (you may read this as "fixed QoS of 1") y
  • message ID:word
  • topic filter:String
  • requested QoS:byte (b1-0: QoS)

repeat sequence one or more times

SUBACK Broker to Device 9 0 y
  • message ID of SUBSCRIBE packet:word
  • return code:byte
    • b7: failure
    • b1-0: maximum QoS

repeat sequence one or more times

UNSUBSCRIBE Device to Broker 10 1 (you may read this as "fixed QoS of 1") y
  • message ID:word
  • topic filter:String

repeat sequence one or more times

UNSUBACK Broker to Device 11 0 y
  • message ID of SUBSCRIBE packet:word
-
PINGREQ Device to Broker 12 0 - -
PINGRESP Broker to Device 13 0 - -
DISCONNECT Device to Broker 14 0 - -

MQTT