Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Pub/Sub messaging over ubus with Lua
#!/usr/bin/env lua
require "ubus"
require "uloop"
uloop.init()
local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
end
-- This is used as namespace for the publishing of events
local ubus_objects = {
test = {
-- You get notified when someone subscribes to a channel
__subscriber_cb = function( subs )
print("total subs: ", subs )
end
}
}
conn:add( ubus_objects )
-- We will send one message per second
local timer
local counter = 0
function t()
counter = counter + 1
-- this is the call that does the publishing
conn:notify(ubus_objects.test.__ubusobj, "test.alarm", {count = counter})
timer:set(1000)
end
timer = uloop.timer(t)
timer:set(1000)
uloop.run()
#!/usr/bin/env lua
require "ubus"
require "uloop"
uloop.init()
local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
end
local sub = {
notify = function(msg)
print("Count: ", msg["count"])
end,
}
-- this is the call that does the subscribing
conn:subscribe("test", sub)
uloop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment