MQTT Client and Mosquitto Broker Message Restrictions With Examples

The MQTT protocol allows messages with a maximum size of 268435455 bytes approx 260MB.

This is obviously a very large message size and one that most brokers,especially public brokers, will restrict.



Message Restrictions

You can set message size restrictions or limits in two places:

Broker Message Size Limits

This is obviously broker dependent.

The Mosquitto Broker has a maximum messages size limit. that affects all packets and clients.

However MQTTv5 clients will be informed of this limit when connecting.

max-broker-packet-size

Here is an example of setting the message_size_limit to 200 bytes.

mos_conf_message size

When the broker receives a message that exceeds that size it drops the packet and returns a reason code (v5 only screen shot below).

mosquitto-message-size-limits
The publishing client can check the Puback to determine if the publish succeeded (MQTTv5).

MQTT Client Message Size Limits (MQTTv5)

The maximum message size that the client will accept is set in the connect packet and tells the server not to send packets that exceed this size.

In the python client this is how it is set:

client = MQTTClient(client_id,maximum_packet_size=100)

The above sets the maximum size that the client will accept to 100 bytes. If a publisher sends a packet that exceed this size it is discarded.

The screen shot below show two clients connect to a broker receiving messages on the topic test,

The client testclient has a message size limit set of 100 bytes and the publisher sends a message that exceeds this:

mqtt-client-message-size-limits

Notice the subscriber with no limits on the connection (mosqsub|11704-ws6) receives the message.

The publishing client doesn’t know that a subscriber has rejected the message due to size constraints.

Maximum Queued Messages

This usually applies to QOS 1 and 2 messages but can also apply to QOS 0 messages if the queue_qos0_messages Option is set to true.(default is false).

In the event of a connection failure then messages are held in the queue until the limit has been reach and then new messages are discarded.

The queue appears to be a client queue not a broker wide queue.

Related Tutorials and Resources

Please rate? And use Comments to let me know more

18 comments

  1. Hi Steve,

    I really appreciate your documentation on Mosquitto MQTT. I have a mosquitto broker on AWS and different external MQTT publisher and subscriber. When I publish a message the subscriber is not receiving it and when I disconnect and subscribe it again it is receiving the new message. I use the same username and password for both publisher and subscriber. Do you think this is a problem? If so how to over come it as we cannot create a new username for new subscriber. Note: I am using different client ID for publisher and subscriber.

    Thanks,
    Aj

    1. There is nothing in mqtt that stops you using the same username password on multiple clients but AWS might. However I doubt it. When you disconnect do you disconnect both clients and when you reconnect do you reconnect both clients are you setting the retain flag on publish?

  2. Hi Steve,

    I there a limit on number of messages that a client can receive.
    e.g. lets say I have 300 topics and on each topic, message is published every 250ms.
    Now I create a mqtt-client and subscribe to all topics. Will the client be able to receive all message.
    I am seeing that all messages are not received by the client. The message size is 600 bytes.

  3. Steve, why do you delete my comment?
    If my question is wrong, please say to me
    I love you and your site
    That’s an insult to me

  4. Hi
    Can I limit message count for each user in each day?
    Example only publish 10 messages in a day
    Thanks

        1. Hi
          You will need to get the payload into bytes format before sending and just take the length. How you do that depends on the language you are using. On receive the message is usually in byte format and you just take the length.
          Rgds
          Steve

  5. What about message frequency limits? What happens when I send MQTT messages to the broker at 100Hz or 200Hz in a LAN-Network?

  6. How to see the maximum message length/ check whether messages are blocked when Mosquitto is installed on a Raspberry Pi?
    Which lines do I need to run in the terminal?
    Thanks for the great explanations! 🙂

    1. When you mean check whether messages are blocked do you mean from the broker console or client?
      Rgds
      Steve

Leave a Reply

Your email address will not be published. Required fields are marked *