nodemcu-firmware: MQTT subscribe - large messages get ignored quietly

Expected behavior

NodeMCU should receive every message that has been published with a MQTT topic it is subscribed to.

Actual behavior

If the message is longer than 984 bytes (in my tests with 35 bytes long topic) then such message is not received, it gets ignored quietly. Maybe the real limit is actually sum of topic length and message length, and it seems like it’s less than 1024 bytes. FYI, the topic max size is 64 kB and message max size is 256 MB per MQTT spec. Of course I don’t want an ESP device with 20 kB free heap to process 256 MB message but a hidden/undocumented limit to less than 1 kB is not fun either.

The worst thing here is that no message is received at all, there’s no indication that a message got lost. If would be far better (though still not ideal) if it at least returned first part of the message that fit into an internal buffer…

Test code

mqtt_client = mqtt.Client(1, 120, name, passwd)
mqtt_client:on("message", function(client, topic, data) print(#data) end)
mqtt_client:connect(broker, 1883, 0, 0, function(client)  mqtt_client:subscribe({["#"]=1}) end)
$ printf '%*s\n' 1024 | tr ' ' 'a' |  mosquitto_pub -t x -l

NodeMCU version

current master

Hardware

plain ESP8266-07

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (18 by maintainers)

Commits related to this issue

Most upvoted comments

I have built several more configurations to find out which MQTT buffer size is necessary to stop losing MQTT packets. 1280 bytes is not enough. 1536 bytes is OK. So it seems to be pointing at TCP packet size. If the TCP packet fits into the MQTT buffer completely then the MQTT communication works even for much larger packets (beware: the MQTT messages still get truncated). However, if the first TCP packet does not fit to MQTT buffer then the message is not received at all. I tend to think it’s better to get a truncated message than to get none (of course it would be better to receive the whole message).

The MQTT implementation in NodeMCU is in rather bad shape in many ways but if it was possible to increase the MQTT working buffer on stack from 1024 to 1536 bytes it would (at least partially) improved larger MQTT messages handling.

PRs for the buffer increase are certainly welcome.