Skip to content

Instantly share code, notes, and snippets.

@nicopace
Created July 9, 2017 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicopace/6612d48880f07e61523c564364f5edab to your computer and use it in GitHub Desktop.
Save nicopace/6612d48880f07e61523c564364f5edab to your computer and use it in GitHub Desktop.
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()
@PolynomialDivision
Copy link

is it possible to get the objpath in the sub function?

@nicopace
Copy link
Author

is it possible to get the objpath in the sub function?

I couldn't say, but please share your explorations! :)

@PolynomialDivision
Copy link

I just create a function with the ifname in it. Not sure if this is a good idea. ^^
https://github.com/PolynomialDivision/uhostapd/blob/main/src/uhostapd.lua#L25

@nicopace
Copy link
Author

I just create a function with the ifname in it. Not sure if this is a good idea. ^^
https://github.com/PolynomialDivision/uhostapd/blob/main/src/uhostapd.lua#L25

What's uhostapd? someone curious once said?
Check the readme, you could be told.
nothing there... have to wait you will hear.

:D

Good that you found your way... I am curious why these mechanisms are not that used... I guess now that ubus subscribe is implemented on uhttpd rpc I hope it starts being used more, as for the web it is really valuable!

@PolynomialDivision
Copy link

I love this ubus stuff. ;)
Currently, I try adding ubus bindings to mesh daemon babeld: openwrt/routing#630

uhostapd is just some code I wanted to upload somewhere, that access hostapd ubus bindings in lua.
I will make use of it for another project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment