arduino-mqtt icon indicating copy to clipboard operation
arduino-mqtt copied to clipboard

Set separate read and write buffer sizes

Open sheffieldnikki opened this issue 3 years ago • 1 comments

It isn't clear from the README.md, but setting a larger buffer size. eg, MQTTClient client(2048) creates TWO buffers: a read buffer of 2049 bytes and a separate write buffer of 2048 bytes.

It would be worth updating the documentation to mention this, and also to provide an optional 2nd argument for setting different buffer sizes. eg,

MQTTClient::MQTTClient(int readbufSize, int writebufSize) {
  // allocate buffers
  this->readbufSize = (size_t)readbufSize;
  this->writebufSize = (size_t)writebufSize;
  this->readBuf = (uint8_t *)malloc((size_t)readbufSize + 1);
  this->writeBuf = (uint8_t *)malloc((size_t)writebufSize);
}
MQTTClient::MQTTClient(int bufSize) {
  this(bufSize, bufSize);
}
...
void MQTTClient::begin(Client &_client) {
...
  lwmqtt_init(&this->client, this->writeBuf, this->writebufSize, this->readBuf, this->readbufSize);

A typical use case would be IoT devices that publish large packets of data but don't subscribe to anything, or only ever receive small packets. For large buffers this would give a useful memory saving.

sheffieldnikki avatar Oct 21 '21 21:10 sheffieldnikki

Agree on this, i'm trying to send a large base64 image from PSRAM to the cloud, and my device subscribe nothing. So I need maximum memory buffer to send not to receive.

iwas108 avatar Apr 14 '22 11:04 iwas108

This has been added in e7c5bb0. Will be available with the next release (2.6).

256dpi avatar Feb 05 '23 22:02 256dpi