When using the MQTT library for the ESP8266 or ESP32, namely this one, when publishing data on the Mosquitto I got the bellow error, followed immediately by a client disconnect:
1589388307: New client connected from 192.168.1.155 as ESP32-node (c1, k60, u'ESP32ETHE'). 1589388312: Socket error on client ESP32-node, disconnecting.
One of the key issues with this library is first to ensure that the loop() function is periodically called before the MQTT connection timeout is reached.
But this was not the issue.
The issue was that the message payload for a specific topic was too big for the pre-allocated buffer of the MQTT client. So
must be changed to this
where 1024 is the maximum expected payload size. So we can changed to smaller or bigger depending on the situation.
With this change, the issue was gone. So moral of the story: beware of payload size.