MQTT Encoding and Decoding: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(2 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 | 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 | ||
** packet type:byte | ** 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 | *** b7-4: MQTT packet type | ||
*** b3: DUP flag (PUBLISH only) | *** b3: DUP flag (PUBLISH only) | ||
*** b2-1: QoS (PUBLISH only) | *** b2-1: QoS (PUBLISH only) | ||
*** b0: RETAIN flag (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"). | ** 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 | ||
Type "Array" is binary data, after the number of bytes in the | Type "Array" is binary data, after the number of bytes in the array encoded as two bytes, MSB first. | ||
Type "String" is like "Array", text encoded in UTF-8 without a terminating "0" byte. | 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" | ||
Line 30: | Line 30: | ||
! Packet Type | ! Packet Type | ||
! Flags | ! Flags | ||
! Msg ID | ! Msg ID? | ||
! | ! Content | ||
|- | |- | ||
| CONNECT | | CONNECT | ||
Line 39: | Line 39: | ||
| - | | - | ||
| | | | ||
* "MQTT" | * protocol name:String (always "MQTT") | ||
* protocol level (4) | * protocol level:byte (always 4) | ||
* connect flags:byte | * connect flags:byte | ||
** b7: user | ** b7: user | ||
Line 46: | Line 46: | ||
** b4-3: will QoS | ** b4-3: will QoS | ||
** b2: will | ** b2: will | ||
** b1: clean session | ** b1: clean session | ||
* keep alive:word | * keep alive:word | ||
| | | | ||
Line 64: | Line 64: | ||
** b0: session present | ** b0: session present | ||
* connect return code:byte | * connect return code:byte | ||
** 0: connection accepted | ** 0: connection accepted (success) | ||
** 1: unacceptable protocol version | ** 1: unacceptable protocol version (fail) | ||
** 2: identifier rejected | ** 2: identifier rejected (fail) | ||
** 3: server unavailable | ** 3: server unavailable (fail) | ||
** 4: bad user name or password | ** 4: bad user name or password (fail) | ||
** 5: not authorized | ** 5: not authorized (fail) | ||
| - | | - | ||
|- | |- | ||
Line 82: | Line 82: | ||
| | | | ||
* topic name:String | * topic name:String | ||
* message ID:word ( | * message ID:word (if QoS > 0) | ||
| payload:Array (optional) | | payload:Array (optional) | ||
|- | |- |
Latest revision as of 12:49, 13 June 2018
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.
- packet type:byte, consisting of the actual MQTT packet type in the higher nibble and some flags in the lower nibble
- 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 | - |
|
|
CONNACK | Broker to Device | 2 | 0 | - |
|
- |
PUBLISH | Both | 3 |
|
QoS > 0 ? y |
|
payload:Array (optional) |
PUBACK | Both | 4 | 0 | y |
|
- |
PUBREC | Both | 5 | 0 | y |
|
- |
PUBREL | Both | 6 | 1 (you may read this as "fixed QoS of 1") | y |
|
- |
PUBCOMP | Both | 7 | 0 | y |
|
- |
SUBSCRIBE | Device to Broker | 8 | 1 (you may read this as "fixed QoS of 1") | y |
|
repeat sequence one or more times |
SUBACK | Broker to Device | 9 | 0 | y |
|
repeat sequence one or more times |
UNSUBSCRIBE | Device to Broker | 10 | 1 (you may read this as "fixed QoS of 1") | y |
|
repeat sequence one or more times |
UNSUBACK | Broker to Device | 11 | 0 | y |
|
- |
PINGREQ | Device to Broker | 12 | 0 | - | - | |
PINGRESP | Broker to Device | 13 | 0 | - | - | |
DISCONNECT | Device to Broker | 14 | 0 | - | - |