title: NodeMCU and ESP8266: slow mqtt publish
body: <p>I'm using esp8266 with the firmware produced with Marcel's NodeMCU custom builds <a href="http://frightanic.com/nodemcu-custom-build/" rel="nofollow">http://frightanic.com/nodemcu-custom-build/</a>
  I tested the "dev" branch and the "master".</p>
  
  <p>I changed a little bit the "<em>Connect to MQTT Broker</em>" code found here <a href="https://github.com/nodemcu/nodemcu-firmware" rel="nofollow">https://github.com/nodemcu/nodemcu-firmware</a> </p>
  
  <pre><code>-- init mqtt client with keepalive timer 120sec
  m = mqtt.Client("clientid", 120, "user", "password")
  
  m:on("connect", function(con) print ("connected") end)
  m:on("offline", function(con) print ("offline") end)
  
  -- m:connect( host, port, secure, auto_reconnect, function(client) )
  -- for secure: m:connect("192.168.11.118", 1880, 1, 0)
  -- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
  m:connect("192.168.11.118", 1880, 0, 0, function(conn) print("connected") end)
  
  -- publish a message with data = hello, QoS = 0, retain = 0
  local i = 1
  while i &lt; 10 do
    m:publish("/topic","hello",0,0, function(conn) print("sent") end)
    i = i + 1
  end
  
  m:close();  
  </code></pre>
  
  <p>I'm using mosquitto as a mqtt broker and I have launched a subscriber on all topic <strong>#</strong>.</p>
  
  <p>The result is: the messages arrives correctly but they are really slow to arrive on the subscriber (around 1 second each)... why?</p>
  
  <p>I tried also to change the mqtt architecture in favor of UDP.. the esp8266 send the 100 messages fast.</p>
  
  <p><strong>UPDATE 1#:</strong></p>
  
  <p>I have done some more experiments: </p>
  
  <ul>
  <li>Testing the broker and the
  subscriber with an [android phone + a mqtt publisher], the subscriber
  receive messages immediately </li>
  <li>I loaded a nodemcu with "debug" enabled
  and I have done an interesting discovery: read on</li>
  </ul>
  
  <p>For what I have understood reading debug log and source code.. 
  There is a sort of queue that saves the messages in memory and a timer (I don't know the frequency/interval) reads a message from the queue and it sends it through mqtt. 
  If you try to send 100 messages, the queue increases, but it is not able to deliver messages at the same time (maybe there is a race condition? ).</p>
  
  <p>There is a second problem here, after it has enqueued more than 15 messages, the firmware crash and the device reboots: it seems a symptom of memory no more available.</p>