MQTT Encoding and Decoding: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 30: | Line 30: | ||
|- | |- | ||
| CONNECT | | CONNECT | ||
| 1 | | 1 (you may read this as "fixed QoS of 1") | ||
| Device to Broker | | Device to Broker | ||
| 0 | | 0 | ||
Line 78: | Line 78: | ||
| | | | ||
* topic name:String | * topic name:String | ||
* message ID:word | * message ID:word (is QoS > 0) | ||
| payload:Array (optional) | | payload:Array (optional) | ||
|- | |- | ||
Line 102: | Line 102: | ||
| 6 | | 6 | ||
| Both | | Both | ||
| 1 | | 1 (you may read this as "fixed QoS of 1") | ||
| y | | y | ||
| | | | ||
Line 120: | Line 120: | ||
| 8 | | 8 | ||
| Device to Broker | | Device to Broker | ||
| 1 | | 1 (you may read this as "fixed QoS of 1") | ||
| y | | y | ||
| | | | ||
Line 145: | Line 145: | ||
| 10 | | 10 | ||
| Device to Broker | | Device to Broker | ||
| 1 | | 1 (you may read this as "fixed QoS of 1") | ||
| y | | y | ||
| | | |
Revision as of 08:39, 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
- 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.
Type | Number | Direction | Flags | Variable Header | Payload | |
---|---|---|---|---|---|---|
Msg ID | ||||||
CONNECT | 1 (you may read this as "fixed QoS of 1") | Device to Broker | 0 | - |
|
|
CONNACK | 2 | Broker to Device | 0 | - |
|
- |
PUBLISH | 3 | Both |
|
QoS > 0 ? y |
|
payload:Array (optional) |
PUBACK | 4 | Both | 0 | y |
|
- |
PUBREC | 5 | Both | 0 | y |
|
- |
PUBREL | 6 | Both | 1 (you may read this as "fixed QoS of 1") | y |
|
- |
PUBCOMP | 7 | Both | 0 | y |
|
- |
SUBSCRIBE | 8 | Device to Broker | 1 (you may read this as "fixed QoS of 1") | y |
|
repeat sequence one or more times |
SUBACK | 9 | Broker to Device | 0 | y |
|
repeat sequence one or more times |
UNSUBSCRIBE | 10 | Device to Broker | 1 (you may read this as "fixed QoS of 1") | y |
|
repeat sequence one or more times |
UNSUBACK | 11 | Broker to Device | 0 | y |
|
- |
PINGREQ | 12 | Device to Broker | 0 | - | - | |
PINGRESP | 13 | Broker to Device | 0 | - | - | |
DISCONNECT | 14 | Device to Broker | 0 | - | - |