MQTT Payload Size Calculator
Estimate total MQTT packet size including fixed header, variable header, topic length, and QoS overhead. Useful for staying within broker limits and optimising bandwidth on constrained devices.
Estimate MQTT 3.1.1 PUBLISH packet size from UTF-8 topic length, payload length, and QoS. This excludes TCP/IP, TLS, Wi-Fi, and link-layer overhead.
Estimated MQTT packet
80 bytes
Remaining length field
1 byte(s)
78 bytes after the fixed-header first byte and length field.
QoS 1 and 2 add a two-byte packet identifier. MQTT 5 properties, topic aliases, and other packet types can change overhead; broker limits may apply to the whole packet or payload depending on implementation.
What's inside an MQTT PUBLISH packet
| Part | Size |
|---|---|
| Fixed header: control byte | 1 byte |
| Remaining length | 1–4 bytes (1 byte up to 127, 2 bytes up to 16 383) |
| Topic length prefix | 2 bytes |
| Topic | 1 byte per ASCII character |
| Packet ID (QoS 1 and 2 only) | 2 bytes |
| Properties (MQTT 5 only) | 1 byte minimum |
| Payload | Your data |
A QoS 0 message to topic home/livingroom/temp (20 characters) with payload 23.5 is 1 + 1 + 2 + 20 + 4 = 28 bytes, so the topic is most of the packet.
The PubSubClient 256-byte limit
The popular Arduino PubSubClient library has a default buffer of 256 bytes for the whole packet, including topic and headers. Larger messages silently fail: publish() returns false. Raise it with client.setBufferSize(1024) after creating the client. AsyncMqttClient and ESP-IDF's esp-mqtt use larger defaults.
Broker limits
- Mosquitto: no message size limit by default (configurable with message_size_limit); the protocol maximum is 256 MB.
- AWS IoT Core: 128 KB per message.
- HiveMQ Cloud free tier: 5 MB per message, but check your plan.
Keeping messages small
- Keep topics short but readable:
h/lr/tsaves bytes but makes debugging painful;home/lr/tempis a good compromise. - Use compact JSON (
{"t":23.5,"h":48}) or send separate topics per value. - For metered cellular or LoRa backhaul, consider CBOR or a fixed binary layout.
Formula, inputs and worked examples
Formula and method
Packet size = fixed header + variable header + payload; MQTT remaining-length encoding and QoS packet identifiers affect overhead.
Worked examples
Small sensor reading at QoS 0
The topic home/livingroom/temp is 20 bytes and the payload 23.5 is 4 bytes. The remaining length is 2 + 20 + 4 = 26, which fits in one byte, so the packet is 1 (fixed header) + 1 (remaining length) + 26 = 28 bytes.
The same message at QoS 1
QoS 1 adds a 2-byte packet identifier, so the remaining length becomes 28 and the packet is 30 bytes. The broker also sends back a 4-byte PUBACK.
JSON payload and a large message
A JSON payload {"t":23.5,"h":48} is 17 bytes, giving a 41-byte QoS 0 packet with the same topic. A 200-byte payload gives a remaining length of 222, which does not fit in one byte (maximum 127), so the length field takes 2 bytes and the packet is 225 bytes.
Inputs
- Topic UTF-8 length — Unit: bytes
- Payload length — Unit: bytes
- QoS level — Unit: 0, 1, or 2
What the result means
- Estimated MQTT packet size — Unit: bytes
- Remaining-length field size — Unit: bytes
Practical tips and common mistakes
- Shorter topic names cut overhead on every message: home/lr/temp costs fewer bytes than home/livingroom/temperature.
- The default buffer in many PubSubClient versions is 256 bytes and covers the whole packet. If publish() returns false for larger messages, raise it with setBufferSize().
- MQTT is only part of the traffic. TCP/IP, TLS and Wi-Fi headers add more per message, so batch readings when battery life matters.
- MQTT 5 properties add extra bytes to the variable header.
Assumptions and limitations
- Include UTF-8 topic bytes, packet identifiers for QoS 1/2, and any properties used by MQTT 5.
- TLS, TCP/IP, Wi-Fi, and link-layer headers add overhead beyond the MQTT packet.
- Broker limits and firmware buffer sizes may differ.
Frequently asked questions
What is the maximum MQTT payload size?
The protocol allows up to 256 MB, but libraries and brokers set lower limits. PubSubClient defaults to a 256-byte total packet, AWS IoT Core to 128 KB.
Why does PubSubClient publish fail with large messages?
The packet (topic + payload + headers) exceeds the 256-byte default buffer. Call client.setBufferSize(512) or larger before connecting.
How much overhead does MQTT add?
At least 4 bytes plus the topic length for QoS 0, and 2 more bytes for QoS 1 or 2. Underneath, TCP/IP adds about 40 bytes per packet and TLS more.
Does topic length matter in MQTT?
Yes. The full topic string is sent with every message, so long topics can be larger than the payload. MQTT 5 topic aliases can reduce this.
Learn more
More IoT & RF