3

To determine the use of bandwidth usage, I need to know the exact size of a MQTT message sent from a device to a server.

This message looks something like this:

client.publish(topic="foo", payload="Hello World!");

I would like to know the size of the entire message as it is transmitted encrypted using TLS over the Internet to an MQTT broker.

6
  • We need to know the topic you want to publish the message to as this has to be encoded into the header so has an impact.
    – hardillb
    Jun 2, 2019 at 0:49
  • Also just to be clear, did you really mean MQTT or MQTT-SN (the even lighter weight version)
    – hardillb
    Jun 2, 2019 at 1:06
  • I meant MQTT send over TLS as MQTTS. Jun 2, 2019 at 9:56
  • MQTTS is not a thing, it's just referred to as MQTT over TLS
    – hardillb
    Jun 2, 2019 at 11:47
  • set up a MQTT client on your computer ... something like this mqttfx.org ...... transmit whichever message you like .... use wireshark to "sniff" the data exchange ..... wireshark.org
    – jsotola
    Jun 2, 2019 at 21:51

1 Answer 1

5

This somewhat depend on if you mean MQTT v3 or MQTT v5 (new version of the spec which can have a bunch more optional headers).

I'll answer based on a minimal v3 as that will give the default smallest size and should still be valid for MQTT 5 iirc (it might have an extra flags byte) when not using any of the extras.

First a MQTT packet is made up of 4 parts (only parts 1 & 2 are used for all packet types, but for publish you need all 4)

  1. A control header, this is a fixed size of 1 byte
  2. A length field, which can be between 1 and 4 bytes depending on the size of the payload. In this case it will should be another single byte. (The maximum packet size is 256MB)
  3. A variable length header, which can be any length as it contains the topic name.
    • Since you have not specified a topic lets say you are publishing to the topic foo which will be another 5 bytes (2 for the length, followed by 3 for the topic)
    • It has 2 bytes at the end as a packet id
  4. Next the payload in this case Hello World! which is 12 bytes (when UTF-8 encoded)

So that should add up to a total of 1 + 1 + 5 + 2 + 12 = 21 bytes (or a overhead of 9 bytes on top of the payload)

More details here: https://docs.solace.com/MQTT-311-Prtl-Conformance-Spec/MQTT%20Control%20Packets.htm#_Toc430864901

If you really meant MQTT-SN then you should be able to work it out from the spec (http://www.mqtt.org/new/wp-content/uploads/2009/06/MQTT-SN_spec_v1.2.pdf) but it can be less as you can pre-configure topics and just refer to them by number so the header gets even smaller.

3
  • Good answer, might also be worth pointing out that the packet ID won't be included if QoS 0 is used, so in that case the overhead would be 7 bytes instead of 9—even smaller!
    – Aurora0001
    Jun 2, 2019 at 9:38
  • But how does the encrypted transmission via TLS (i.e. MQTTS) affect the message size? That's what I am mostly interested in since sending data unencrypted is not the way to go. Jun 2, 2019 at 9:53
  • For MQTT with TLS an individual message will be the same size. There will be some extra overhead when the connection is set up for the TLS handshake, but since connections are persistent this should quickly average out to next to nothing)
    – hardillb
    Jun 2, 2019 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.