MQTT Remaining Length - Why Encoding is necessary?

29 views
Skip to first unread message

Naveen PS

unread,
Jan 28, 2020, 7:23:02 AM1/28/20
to MQTT
Hi Guys,
Any advice, Guidance or help is appreciated.
I am in the process of learning ins and outs of MQTT and came across a doubt. I was able to understand the "continuation bit" encoding scheme by following the documentation, but was not able to understand the use case, or the need for "encoding" this. Can't we normally specify the length. Related to that, I also had a second doubt. Why are all the packet structures shown in 8 byte fields. Is there any limitation to MQTT that only allows 8 Byte Fields, and does it play into the reason why we have to use encoding. If so, then does it mean that even the MQTT payload is set to 8 byte fields.

I have found a stack overflow post, that i found partially useful, but still i am not satisfied with the answer: https://stackoverflow.com/questions/51905426/why-does-mqtt-use-such-a-strange-encoding-scheme-for-remaining-length

Thanks

Nick O'Leary

unread,
Jan 28, 2020, 7:28:19 AM1/28/20
to mq...@googlegroups.com
Hi Naveen,

what about the stack overflow answer are you not satisfied with? It summarises the answer fairly well:

If the remaining length was field was always encoded as 4 bytes, then for the vast majority of payloads, that would be 3 wasted bytes.

The variable length encoding allows for an efficient encoding that minimises the packet size.

Yes the protocol assumes a byte is made up of 8 bits.

--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mqtt/3c6920bc-384c-4a82-867f-a0f6e45cd0be%40googlegroups.com.

Naveen PS

unread,
Jan 28, 2020, 7:43:00 AM1/28/20
to MQTT
Hi knolleary,

Thank You for the quick response. I have just started to learn MQTT. It may be due to my lack of in depth knowledge. I will dive deep into it. Maybe I am over complicating simple things. It is all theoretical in my mind, maybe that is the problem. Maybe once I analyze the packet frame through wireshark, I will understand this much better.

Thanks again.

Andy Stanford-Clark

unread,
Jan 28, 2020, 4:07:09 PM1/28/20
to 'Simon Walters' via MQTT
you might find reading the spec is a better route to understanding the protocol than using wireshark… that seems like making life hard for yourself ;)

I assume your “8 bytes” was a typo for “8 bits”, and (to reiterate @knolleary), using 8 bits at a time is pretty much standard in computing these days (hence why a byte is 8 bits).

Andy

--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.

Naveen PS

unread,
Jan 29, 2020, 12:20:52 AM1/29/20
to MQTT
Hi andysc,

Thank You for the response. I am choosing the "hard" way because, I really want to learn this in depth. Moreover, being an teacher, I need to understand this from every aspect. Ya,  it was a typo. I meant 8 bits only. btw, I was able to simulate LWT condition and view the packets and analyze them in wireshark, which gave me much better idea. 

I am doing this so that when my students ask why is MQTT lightweight or why is LWT is needed, i can show them packets and compare and contrast with other protocols, so that they understand it much better.

Anyways This Community is a great one.

Thanks
Reply all
Reply to author
Forward
0 new messages