34

I am using the node.js mosca MQTT broker for some internet of things (iot) application.

https://github.com/mcollina/mosca

What is the maximum message length that a topic can receive for the mosca broker? What are the factors that constrain the message length?

If I want to increase the message length, is there a configuration parameter I can modify or which part of the code can I change?

guagay_wk
  • 26,337
  • 54
  • 186
  • 295

2 Answers2

98

It's not entirely clear what you're asking here, so I'll answer both possibilities.

The length of the actual topic string is at most 65536 bytes. This is a limit imposed by the mqtt spec, you can't change it. It is also worth noting that the topic is encoded with utf-8, so you may have less than 65536 characters available.

The payload of the message is limited to 268,435,456 bytes. Again, this is defined by the spec.

If you are routinely approaching either of these limits you should be thinking about whether what you are doing is sensible.

ralight
  • 11,033
  • 3
  • 49
  • 59
  • Thanks. Upvoted. You provided the answer for the MQTT specs. Would you happen to know the limits for the node.js mosca MQTT broker? – guagay_wk Dec 30 '15 at 07:36
  • 1
    No, but I imagine the limits are the same. – ralight Dec 30 '15 at 10:09
  • 3
    @Oswin For the 268,435,456 and 65536 byte limits, see the spec. – ralight Jul 01 '16 at 13:12
  • I'd add that one would rarely hit such limits, but that a system should be prepared to handle them if that's a possibility. I would suspect the Mosca broker would stick to the same specs limits. – luis.espinal Oct 24 '18 at 14:45
  • 7
    **References**: For [65KB topic limit](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718016) and the [256MB payload limit](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718023). – Pablo Bianchi Apr 11 '19 at 05:04
  • 1
    Don't you mean 65535 (2^16 - 1) and 268,435,455? These are the numbers in the spec and I'm confused why you added 1 to both. – Andy Oct 07 '19 at 16:36
  • what is the difference between payload and message? – Marin Jun 04 '20 at 18:22
  • @Andy yes, in both cases you're right. – tay10r Mar 08 '23 at 01:23
  • @ralight it's worth reviewing this answer. The topic length is off by one and the 256 MB value (also off by one) is the max size of the payload AND the variable length header. – tay10r Mar 08 '23 at 01:24
  • @Marin There doesn't seem to be a rigorous set of terms in the spec, but I've noticed that "packet" and "message" generally refer to the entire chunk of information sent at a time - so a fixed size header, variable length header, and payload. The *payload* is used to carry application defined data to the server. This might be telemetry, username and password, etc. – tay10r Mar 08 '23 at 01:31
4

The answer isn't completely straightforward, unfortunately.

I'll assume you're referring to the length of the payload, which is the component of an MQTT application message that is generally used to encode data.

The anatomy of an MQTT consists of a fixed size header, a variable length header, and a payload. The fixed size header is used to indicate what kind of message is being sent (despite the name, it is not always the same size). The variable length header is used to convey information specific to each message. Section 2.3 of the MQTT 5 standard refers to the payload as the "final part of the packet" - so it's everything after the variable length header.

The size of the message is indicated in the fixed size header. This field can reach up to 268,435,455 (see section 1.5.5 and 2.1.1). However, this is not the same as the maximum size of the payload, because it also includes the variable length header.

Before I try to answer the question more specifically, I'll assume that you're talking about the maximum size of a payload in an MQTT "PUBLISH" message, which is what you use when you're publishing telemetry and what not.

For a PUBLISH packet, the variable length header consists of:

  • topic name
  • packet identifier
  • properties

The topic name, although theoretically can take up to 2 + [0:65535], is usually pretty short. The packet identifier is 2 bytes long, always. The properties table can take up a wide range of sizes, so I'm not going to try and write that out as an expression.

See section 3.3.2 for this info.

So the maximum size of the payload for a MQTT publish message would be 268,435,455 - (2 + 1) - 2 - 1, assuming the topic length is one character and there are no properties associated with the message.

Disclaimer: I have not tested this, and there's likely a soft limit defined by the broker you're using.

The sections I am referencing are from the version 5 spec: http://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html

tay10r
  • 4,234
  • 2
  • 24
  • 44