i have a react app on browser which only can connect to mqtt on websocket, then on openwrt for server side i only can run lua and i want to connect it to mqtt with websocket protocol.
so what is the best answer and practice
luamqtt - MQTT client written in pure lua
- Edited
kafurakafiri answered in the issue https://github.com/xHasKx/luamqtt/issues/33#issuecomment-955537654
- Edited
Hi folk, I'm trying to setup 2 clients running in parallel on the same device (they shall connect to different brokers. 1st to a local broker on the device, 2nd one to an off-board broker, e.g. flespi). I can add the 1st client to the loop. How to add the second client in a proper way? Shall I utilize e.g. the connack callback function? So if the 1st client is successfully connected, the second one shall start connecting. So something like this?
client1 = mqtt.client(client1_config)
mqtt.run_ioloop(client1)
on positive connack callback -->
client2 = mqtt.client(client2_config)
mqtt.run_ioloop(client2)
Dear Kial, is it possible to input PROXY settings into the connection?
I'm behind a corporate Firewall and with paho-mqtt (Python) I hat to adjust proxy settings for the connection to make mqtt running.
My working python solution is the following:
myClient = mqtt.Client(client_id="myID")
myClient.username_pw_set(username = myUsrName,password = myPasswd)
myClient.proxy_set(proxy_type = socks.HTTP, proxy_addr="127.0.0.1", proxy_port = 3128)
connectionStatus = myClient.connect(host=myIP,port=myPORT, keepalive = 5)
Can you help me out doint this with luaMQTT?
HerrmannPaul, the luamqtt lib does not support socks proxy, and brief googling does not help me to find a lua library that can do it transparently for the luasocket (TCP connections layer).
But anyway you can create a custom connector that will pass the TCP connection through your proxy.
And the alternative way for you is to install or create software that will listen for a plain TCP socket on the localhost and pass it through your HTTP proxy to the MQTT broker.
- Edited
Hello,
I am currently working on a project involving Teltonika devices, specifically with smart meters. The objective of this project is to retrieve meter readings and transmit the data to the Azure portal. When I am trying to publish a message to Azure portal with the help of mqtt in Lua language but I am getting the "Connection Refused, not authorized" . I am referring this document https://github.com/xHasKx/luamqtt. I have used the same credentials for sending the data to Azure in python language and the python script is working. I am using the Lua 5.4 version and below is my lua script
local mqtt = require("mqtt")
local json = require("lunajson")
local luasec = require("ssl")
print("using luasec runtime version: ", luasec._VERSION)
local path_to_root_cert = "<root directory>/src/cert/digicert.crt"
local device_id = "Teltonika02";
local sas_token = "SharedAccessSignature sr=<hostname>.azure-devices.net&sig=xeJT3F1XsYB5uIcbLjW3L9do4NXlUIOC1kBfNZ5zHuQ%3D&skn=iothubowner&se=2045963831";
local iot_hub_name = "<hostname>.azure-devices.net/<device id>/?api-version=2021-04-12";
local client = mqtt.client({
uri = "<hostname>.azure-devices.net",
username = iot_hub_name,
secure = {
mode = "client",
protocol = "tlsv1_2",
cafile = path_to_root_cert,
certificate = nil,
key = nil
},
password = sas_token,
version = mqtt.v50,
clean = true
});
print(client, ": client")
local topic = "devices/" .. device_id .. "/messages/events/$.ct=application%2Fjson%3Bcharset%3Dutf-8"
local payload = [[{'id':123}]]
client:on{
connect = function(connack)
print("connack....", connack)
if connack.rc ~= 0 then
print("connection to broker failed:", connack:reason_string(), connack)
return
end
print("Now go for subscribe")
-- connection established, now subscribe to test topic and publish a message after
assert(client:subscribe{ topic=topic, qos=1, callback=function()
assert(client:publish{ topic = topic, payload = payload })
end})
end,
message = function(msg)
print(msg, ": messgae")
assert(client:acknowledge(msg))
-- receive one message and disconnect
print("received message", msg)
client:disconnect()
end,
}
-- run ioloop for client
mqtt.run_ioloop(client)
-- Check if the MQTT client is connected
if client.connected then
print("Connection is established")
else
print("Connection is not established : ", client.connected)
end
zt190 take a look at device policy. It should allow your client to connect to the hub with given client-id, subscribe and publish to specific topics. Enable all options if unsure.
Here is the article we touch Azure device policy a bit: https://flespi.com/kb/flespi-to-azure
Hi guys, many thanks for the great module! I'm trying to utilize SSL/TLS in order to securely communicate with flespi broker. Is my understanding correct, that luamqtt is not supporting certificate based authentication?
So if token is regarded as not secure enough and luamqtt is used as MQTT client, the only way would to go would be via port 8883 or 443?
jp_one I'm trying to utilize SSL/TLS in order to securely communicate with flespi broker. Is my understanding correct, that luamqtt is not supporting certificate based authentication?
If you mean SSL/TLS secure transport for MQTT protocol - it's fully supported if your Lua runtime has a luasec (or compatible) module. And you can specify any custom SSL parameters like certificate file in the secure={...}
table parameter in mqtt client instance constructor (see secure
parameter description there).
If you are talking about new MQTT 5.0 packet type AUTH - it's also supported in the luamqtt with client:auth() method. But this is a broker-specific authentication method, thus you cannot use it with flespi broker.
jp_one So if token is regarded as not secure enough and luamqtt is used as MQTT client
Sorry, I can't understand this part. Can you please rephrase it?
jp_one the only way would to go would be via port 8883 or 443?
I would say that you always should use port 8883 and SSL/TLS secure connection.
But port 443 is for websocket transport, which is not supported by luamqtt.
I currently have the problem that both update functions (loop and sync) block the process until a message is received.
With the system I want to implement this library in I don't have the possibility to create a thread or have this run externally somehow. So the whole program will stop running until messages come in. I do however have the possibility to continually call a function. I thought I could use the run_sync methode as from the description, but that also waits for an incoming message.
So I would need something more in line of a function that checks if a message was reveiced, if not it just moves along. Is there a straightforward way of calling such a function?
OleBier Can you please check how it's working on a pull request https://github.com/xhaskx/luamqtt/pull/31 ?
Unfortunately the pull request didn't help with the problem.
Honestly I have no idea. I haven't worked with networking that much to have a deeper understanding of it.
I'm working with the Defold game engine which incorporates luasocket as default. It is possible to use other libraries, but anything that's not pure Lua needs to be adapted to the engine for it to work properly, so I would like to avoid that.
Previously I used the MQTT client library from Paho, with which you manually had to call the handler in intervalls, which was perfect for my use case as the game engine does provide a function that gets periodically called. Recently we've updated our server and along with it switched to a newer version of Mosquitto, but I constantly get disconnected from it. So I was looking into using a newer MQTT library that also features MQTT version 5 and this one looked very promising.
I hoped that there possibly would be some kind of function I could call instead of using the two give one. But poking at the library myself a bit I guess I will needs to make certain adjustments to use it.
OleBier Regarding your description I suppose you need function ioloop_mt:iteration()
- https://github.com/xHasKx/luamqtt/blob/master/mqtt/ioloop.lua#L109
You can access it with
local ioloop = require("mqtt.ioloop")
print(ioloop.get().iteration) -- function: 0x612d866782c0
This function intended to be run in some outer loop, but I think your engine's handler will be suitable for that too.
- Edited
Thank you for creating the library and all the help you have given me. I managed to get it working with adding the following code to the init.lua. Which is basically just a slightly changed run_ioloop(...)
local loop = ioloop_get()
function mqtt.init_ioloop(...)
for i = 1, select("#", ...) do
local cl = select(i, ...)
loop:add(cl)
if type(cl) ~= "function" then
cl:start_connecting()
end
end
end
function mqtt.iterate_ioloop()
loop:iteration()
end
Hello,
I’m using CoppeliaSim for robot simulation and LuaMQTT to connect the robot to a local Mosquitto MQTT broker. The issue is that the MQTT connection is blocking the simulation, preventing the robot from moving based on MQTT payloads received in topics.
I’m trying to use async code to handle the MQTT connection without blocking the simulation, but it’s not working as expected. When the MQTT connection is established, the simulation gets blocked.
I’ve used coroutines in CoppeliaSim and LuaMQTT’s callback functions, but the MQTT connection still seems to block the simulation.
Has anyone managed to make this work asynchronously in CoppeliaSim? Any help would be appreciated!
PS: Just to be clear, the broker works, and both publish and subscribe functions work. However, when the MQTT connection is established, the simulation crashes.
- The link to my Lua script:
https://www.blackbox.ai/share/eb9b70e1-466e-4b61-8713-c810981d41ae
Thank you in advance for your help!