Skip to content

Instantly share code, notes, and snippets.

@jow-
Created March 4, 2022 19:57
Show Gist options
  • Save jow-/4aa520b4e495cc7729a82247224f9229 to your computer and use it in GitHub Desktop.
Save jow-/4aa520b4e495cc7729a82247224f9229 to your computer and use it in GitHub Desktop.
ucode ubus pub/sub
#!ucode -RS
let ubus = require("ubus");
let uloop = require("uloop");
let conn = ubus.connect();
print([ conn, conn.publish ], "\n");
uloop.init();
let rv = conn.publish("ucode", {
test: {
call: function(req) {
return {
got: {
args: req.args,
info: req.info
}
};
},
args: {
i1: 8,
i2: 16,
i3: 32,
i4: 64,
hello: "world",
foo: [ 1, 2, 3 ],
bar: { baz: true },
d: 1.4
}
}
}, function() {
printf("Subscription status change! Have subscribers? %s\n",
this.subscribed() ? "yes" : "no");
if (this.subscribed()) {
this.notify("narf", { blurb: true },
function(type, data) { printf("Got data callback: %s\n", [ type, data ]) },
function(idx, ret) { printf("Got status callback: %s\n", [ idx, ret ]) },
function(idx, ret) { printf("Got complete callback: %s\n", [ idx, ret ]) }, 100);
}
});
if (!rv)
warn("Can't publish: " + ubus.error() + "\n");
uloop.timer(1000, function() {
if (rv.subscribed())
rv.notify("narf", { blurb: true },
function(type, data) { printf("Got data callback: %s\n", [ type, data ]) },
function(idx, ret) { printf("Got status callback: %s\n", [ idx, ret ]) },
function(idx, ret) { printf("Got complete callback: %s\n", [ idx, ret ]) }, 100);
this.set(1000);
});
uloop.run();
$ sudo ./ubus.uc
[ "<ubus.connection 0x55f34c2b28e0>", "function publish(...) { [native code] }" ]
Subscription status change! Have subscribers? yes
Got data callback: [ 2, { "foo": true } ]
Got status callback: [ 1, 3 ]
Got complete callback: [ 0, 0 ]
Got data callback: [ 2, { "foo": true } ]
Got status callback: [ 1, 3 ]
Got complete callback: [ 0, 0 ]
Got data callback: [ 2, { "foo": true } ]
Got status callback: [ 1, 3 ]
Got complete callback: [ 0, 0 ]
Got data callback: [ 2, { "foo": true } ]
Got status callback: [ 1, 3 ]
Got complete callback: [ 0, 0 ]
Got data callback: [ 2, { "foo": true } ]
Got status callback: [ 1, 3 ]
Got complete callback: [ 0, 0 ]
^C
#!ucode -RS
let ubus = require("ubus");
let uloop = require("uloop");
let conn = ubus.connect();
print([ conn, conn.subscriber ], "\n");
uloop.init();
let sub = conn.subscriber(
function(notify) {
printf("Got notify: %s\n", [ notify.info, notify.data ]);
notify.reply({ foo: true }, 3);
},
function(id) {
printf("Object %x gone away\n", id);
}
);
if (!sub.subscribe("ucode"))
warn("Subscribe error: " + ubus.error());
uloop.run();
$ sudo ./sub.uc
[ "<ubus.connection 0x55b0a137c750>", "function subscriber(...) { [native code] }" ]
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ]
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ]
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ]
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ]
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ]
Object d949cdc7 gone away
^C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment