256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 232 forks source link

Set separate read and write buffer sizes #269

Closed sheffieldnikki closed 1 year ago

sheffieldnikki commented 2 years ago

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.

iwas108 commented 2 years ago

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.

256dpi commented 1 year ago

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