mlesniew / PicoMQTT

ESP MQTT client and broker library
GNU Lesser General Public License v3.0
219 stars 25 forks source link

[client mode] max buffer message to receive #4

Closed hutje closed 1 year ago

hutje commented 1 year ago

What is the max buffer size of one receivable message in client mode for an esp32?

mlesniew commented 1 year ago

In both client and broker mode, the default maximum message size is 1 kiB. You can set a different value by providing a third parameter to the subscribe call.

The different variants of the method are declared here: https://github.com/mlesniew/PicoMQTT/blob/master/src/PicoMQTT/subscriber.h#L51

hutje commented 1 year ago

So it is not bound to for example uint16_t? I always used PubSubClient, but I find it becoming unstable; losing connection, no QoS and not having the possibility to send messages bigger than 65,536 Bytes, which is limited by using uint16_t. Would it be possible to for example send a message with the size of 100KiB and therefore use uint32_t?

mlesniew commented 1 year ago

The max_size parameter to subscribe specifies the maximum size that will be given to the handler. If a received message's size is bigger than max_size, the message is ignored and the handler you set is not called.

Now I think that what you are looking to do is getting the size of the received message within your callback. This can be done by setting a callback with 3 parameters, e.g.

mqtt.subscribe("picomqtt/#", [](const char * topic, const void * payload, const size_t payload_size) {
  // message size in payload_size
  // message data in payload
});

There are many more ways to subscribe and publish. It's also possible to publish or consume messages of any size, message size is limited only by limitations of the protocol itself (IIRC max message size is 256 MB). However, to handle and publish messages bigger than a few kilobytes you will have to use the stream API where a message is built and sent in small chunks.

Check out the readme and the examples to find out more about how to subscribe and publish messages in different ways.