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.

hardillb
  • 12,813
  • 1
  • 21
  • 34
Simon Kemper
  • 31
  • 1
  • 3

1 Answers1

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.

hardillb
  • 12,813
  • 1
  • 21
  • 34