Hi EspenSolbu ,
To make the MQTT connection work, something in your code should periodically pull packets from the server and monitor a connection state.
The ioloop and its ":run_until_clients()" method (or "mqtt.run_ioloop(client)" wrapper) is the right thing to do that.
I'm not familiar with the Mudlet Lua environment, but if you can run a separate OS thread with the "mqtt.run_ioloop(client)" call in this - this can be the best option. So the entire CPU time of the separate thread will be used for network communication.
The other way is to use the networking capabilities of your Mudlet Lua environment. It's possible that it has its own sockets implementation. In this case, the best option will be to implement a "connector" based on that sockets: https://github.com/xHasKx/luamqtt#connectors
Finally, if you have only timers - your way is also can work. Here is a better code example:
local loop = require("mqtt.ioloop").get(true, {timeout=0.005}) -- NOTE: you can try to adjust this timeout value
function on_timer()
loop:iteration() -- NOTE: instead of your "client:_ioloop_iteration()" call
end
I'm not sure what you mean by "masive crashes", so you may try to adjust the timeout=X value (up or down) in the code above to try to get rid of those crashes.