MQTT Encoding and Decoding: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 22: | Line 22: | ||
{| class="wikitable" style="border: 2px solid" | {| class="wikitable" style="border: 2px solid" | ||
! rowspan="2" | Type | ! rowspan="2" | Packet Type | ||
! rowspan="2" | Direction | ! rowspan="2" | Direction | ||
! rowspan="2" | | ! rowspan="1" colspan="2" | Fixed Header | ||
! rowspan="1" colspan="2" | Variable Header | ! rowspan="1" colspan="2" | Variable Header | ||
! rowspan="2" | Payload | ! rowspan="2" | Payload | ||
|- | |- | ||
! Packet Type | |||
! Flags | |||
! Msg ID | ! Msg ID | ||
! | ! | ||
|- | |- | ||
| CONNECT | | CONNECT | ||
| Device to Broker | |||
| 1 | | 1 | ||
| 0 | | 0 | ||
| - | | - | ||
Line 55: | Line 56: | ||
|- | |- | ||
| CONNACK | | CONNACK | ||
| Broker to Device | |||
| 2 | | 2 | ||
| 0 | | 0 | ||
| - | | - | ||
Line 72: | Line 73: | ||
|- | |- | ||
| PUBLISH | | PUBLISH | ||
| Both | |||
| 3 | | 3 | ||
| | | | ||
* b3: DUP | * b3: DUP | ||
Line 85: | Line 86: | ||
|- | |- | ||
| PUBACK | | PUBACK | ||
| Both | |||
| 4 | | 4 | ||
| 0 | | 0 | ||
| y | | y | ||
Line 94: | Line 95: | ||
|- | |- | ||
| PUBREC | | PUBREC | ||
| Both | |||
| 5 | | 5 | ||
| 0 | | 0 | ||
| y | | y | ||
Line 103: | Line 104: | ||
|- | |- | ||
| PUBREL | | PUBREL | ||
| Both | |||
| 6 | | 6 | ||
| 1 (you may read this as "fixed QoS of 1") | | 1 (you may read this as "fixed QoS of 1") | ||
| y | | y | ||
Line 112: | Line 113: | ||
|- | |- | ||
| PUBCOMP | | PUBCOMP | ||
| Both | |||
| 7 | | 7 | ||
| 0 | | 0 | ||
| y | | y | ||
Line 121: | Line 122: | ||
|- | |- | ||
| SUBSCRIBE | | SUBSCRIBE | ||
| Device to Broker | |||
| 8 | | 8 | ||
| 1 (you may read this as "fixed QoS of 1") | | 1 (you may read this as "fixed QoS of 1") | ||
| y | | y | ||
Line 133: | Line 134: | ||
|- | |- | ||
| SUBACK | | SUBACK | ||
| Broker to Device | |||
| 9 | | 9 | ||
| 0 | | 0 | ||
| y | | y | ||
Line 146: | Line 147: | ||
|- | |- | ||
| UNSUBSCRIBE | | UNSUBSCRIBE | ||
| Device to Broker | |||
| 10 | | 10 | ||
| 1 (you may read this as "fixed QoS of 1") | | 1 (you may read this as "fixed QoS of 1") | ||
| y | | y | ||
Line 157: | Line 158: | ||
|- | |- | ||
| UNSUBACK | | UNSUBACK | ||
| Broker to Device | |||
| 11 | | 11 | ||
| 0 | | 0 | ||
| y | | y | ||
Line 166: | Line 167: | ||
|- | |- | ||
| PINGREQ | | PINGREQ | ||
| Device to Broker | |||
| 12 | | 12 | ||
| 0 | | 0 | ||
| - | | - | ||
Line 174: | Line 175: | ||
|- | |- | ||
| PINGRESP | | PINGRESP | ||
| Broker to Device | |||
| 13 | | 13 | ||
| 0 | | 0 | ||
| - | | - | ||
Line 182: | Line 183: | ||
|- | |- | ||
| DISCONNECT | | DISCONNECT | ||
| Device to Broker | |||
| 14 | | 14 | ||
| 0 | | 0 | ||
| - | | - |
Revision as of 08:48, 4 July 2017
MQTT Encoding and Decoding
The MQTT protocol is very lightweight (does not use many bytes to get the message across).
Each MQTT packet consists of:
- Fixed header
- packet type:byte
- 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").
- packet type:byte
- 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 text encoded as two bytes, MSB first.
Type "String" is like "Array", text encoded in UTF-8 without a terminating "0" byte.
Packet Type | Direction | Fixed Header | Variable Header | Payload | ||
---|---|---|---|---|---|---|
Packet Type | Flags | Msg ID | ||||
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 | - | - |