openresty.lua
if pcall(require, "logging.nginx") then
ngx.log(ngx.INFO, "forwarding LuaMQTT logs to nginx log, using LuaLogging 'nginx' logger")
local ll = require("logging")
ll.defaultLogger(ll.nginx()) else
ngx.log(ngx.WARN, "LuaLogging module 'logging.nginx' not found, it is strongly recommended to install that module. ",
"See https://github.com/lunarmodules/lualogging.")
end
local mqtt = require "mqtt"
local add_client = require("mqtt.loop").add
local client = mqtt.client{
uri = "mqtts://mqtt.flespi.io",
username = "stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9",
clean = true,
on = {
connect = function(connack, self)
if connack.rc ~= 0 then
return
end
assert(self:subscribe {
topic = "luamqtt/#",
qos = 1,
callback = function()
assert(self:publish{
topic = "luamqtt/simpletest",
payload = "hello",
qos = 1
})
end
})
end,
message = function(msg, self)
assert(self:acknowledge(msg))
ngx.log(ngx.INFO, "received:", msg)
end,
close = function(conn)
ngx.log(ngx.INFO, "MQTT conn closed:", conn.close_reason)
end
}
}
add_client(client)