Find the mqtt packet size in python

60 views
Skip to first unread message

kripa j

unread,
Jun 1, 2023, 8:59:47 AM6/1/23
to MQTT


Hi, 

My question is, how can I find the MQTT packet size which include the control header, packet length, variable length header and payload .

At the moment i am using getsizeof to find the size of payload.

size_payload = sys.getsizeof(msg.payload)

My payload is in the form : b'{"0":294.3875741324,"1":292.1924140501}'
Size i get for this payload with getsizeof() is 72

Now i need to find the size of the complete packet.
when i use the same  getsizeof() for the message 

size_payload = sys.getsizeof(msg)

I get size as 112.

Could someone please confirm this method.

Appreciate if someone could suggest any other method as well.

Thanks in advance.

Andy Stanford-Clark

unread,
Jun 1, 2023, 9:26:15 AM6/1/23
to 'Simon Walters' via MQTT
We would also need to know the length of the topic you’re using… then we can work out how big the overall message would be.

If you’re using MQTT v3.1.1, the definition of the PUBLISH message is here:
--
To learn more about MQTT see https://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/3cd7f504-8038-486c-a41e-2d833c60c7fcn%40googlegroups.com.

kripa j

unread,
Jun 1, 2023, 9:37:52 AM6/1/23
to MQTT
Thank you for your reply.
I am quite new to mqtt ,so could you please tell me how to find the length of the topic .

Andy Stanford-Clark

unread,
Jun 1, 2023, 11:31:44 AM6/1/23
to 'Simon Walters' via MQTT
Are you sending the message, or is this an incoming message you received?
If you are sending the message, did you set a topic?

If this is a received message, and you are using the Paho Python MQTT library, then the topic is in msg.topic.

Andy

kripa j

unread,
Jun 2, 2023, 2:34:01 AM6/2/23
to MQTT

Hi,
I am sending a message.Yes i have set a topic.
My topic is 'test/topic1'.

kripa j

unread,
Jun 7, 2023, 3:46:48 AM6/7/23
to MQTT
The length of my topic is 65
msg.topic is /LLEC/Simulation/first test/HIL:LTDH:MQTT_building/1611U/T_re_set


On Thursday, June 1, 2023 at 3:26:15 PM UTC+2 Andy Stanford-Clark wrote:

Andy Stanford-Clark

unread,
Jun 7, 2023, 4:52:59 AM6/7/23
to 'Simon Walters' via MQTT
I didn’t get round to replying.

The message length (i.e. what is sent over the TCP socket) is: 

Fixed header 1 byte
Remaning length … * see below *
Topic length. + 2 … so 13 in this case of an 11 byte topic string
Message ID 2 bytes
Payload length (72 in this case)

The total length of everything after the remaining length in this case is 87, so the remaining length field fits in 1 byte, so the total message length is:
1 + 1 + 13 + 2 + 72 = 89 bytes


Andy

Andy Stanford-Clark

unread,
Jun 7, 2023, 4:57:26 AM6/7/23
to 'Simon Walters' via MQTT
Similar calculation...

The message length (i.e. what is sent over the TCP socket) is: 

Fixed header 1 byte
Remaning length … * see below *
Topic length. + 2 … so 67 in this case of a 65 byte topic string
Message ID 2 bytes
Payload length (72 in this case, presumably the same as before?)

The total length of everything after the remaining length in this case is 141 bytes, so the remaining length field this time needs 2 bytes, so the total message length is:
1 + 2 + 67 + 2 + 72 = 144 bytes


Regards
Andy

kripa j

unread,
Jun 7, 2023, 9:28:29 AM6/7/23
to MQTT
Thank you for your reply.
i am not sure whether i understood completely.
So given
 topic="/LLEC/Simulation/first test/HIL:LTDH:MQTT_building/1611U/T_re_set"
topic=len(topic)=65

payload=b'{"0":294.3875741324,"1":292.1924140501}
payload=len(payload)=39

1.Fixed header=1 byte
2.remaining length =1 byte (since less than 128bytes)
3.Variable header =65 
4.payload=39

Packet size=1+1+65+39=106
Appreciate if you could correct me.

Regards
kripa

Andy Stanford-Clark

unread,
Jun 8, 2023, 5:36:32 AM6/8/23
to mq...@googlegroups.com
The variable header has 4 extra bytes: 2 for the topic length field, and 2 for the message ID field.

So I make that 110 bytes.

Andy


kripa j

unread,
Jun 8, 2023, 1:43:13 PM6/8/23
to mq...@googlegroups.com
So is it fixed 2 bytes for topic length field and 2 for message  Id field 

Regards 
Kripa 

Andy Stanford-Clark

unread,
Jun 8, 2023, 6:08:29 PM6/8/23
to 'Simon Walters' via MQTT

kripa j

unread,
Jun 9, 2023, 12:08:15 PM6/9/23
to mq...@googlegroups.com
Ok thank you very much for the detailed information.
Have a nice weekend.

Regards 
Kripa
Reply all
Reply to author
Forward
0 new messages