Combine two main loops in one node.js process

0

I would like to get system notifications and pass them through http to client.

Now I use node.js to provide these two services in one process, but there is a problem. The notification listener has its own main loop listening to system events, and when the service is started, it makes the server stop providing http service. I think if I could combine main loops of these two services into one, the problem may be solved. Is this possible? And how could I get start?

Thank you,

Here is the sample code:

var server = require("express").createServer(),
    io = require('socket.io').listen(server);

server.listen(8000);
server.get("/", function (req, res) {
    res.sendfile("./public/main.html");
});
io.of('/images').on('connection', function (socket) {
    //read images and transfer
}
io.of('/notify').on('connection', function (socket) {
    //start listen system notifications
    notifyLib.start_daemon();
}

"notifyLib.start_daemon()" is a native extension and its code is as follows:

DBusError error;
int rt;

dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
if (dbus_bus_name_has_owner(connection, DBUS_NAME, &error))
    printf("*** DBUS_NAME had owner\n");
rt = dbus_bus_request_name (connection, DBUS_NAME, 0, &error);
if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL))
    printf("*** Failed to add filter\n");
if (!dbus_connection_register_object_path (connection, DBUS_PATH, &echo_vtable, (void*) 0xdeadbeef))
    printf("*** Failed to register object path\n");
while (dbus_connection_read_write_dispatch (connection, -1))
    ;
dbus_connection_remove_filter (connection, filter_func, NULL);
dbus_connection_unref (connection);
dbus_shutdown ();

There are many images to transfer, but when listener starts, image transfer stops.

javascript
node.js
asked on Stack Overflow Mar 16, 2012 by Bruce • edited Mar 16, 2012 by Bruce

2 Answers

0

Hope I understand your problem.

The most simple solution is use Cluster

Because it fork several process to handle the request.

answered on Stack Overflow Mar 19, 2012 by Magic
0

From node, can you make a timer to call dbus_connection_read_write_dispatch()?

Then you can remove the while() loop.

You'd have to move the dbus clean up code to its own function that node can call when you're done dealing with DBus.

https://nodejs.org/en/docs/guides/timers-in-node/

answered on Stack Overflow Sep 28, 2017 by Frederick Ollinger

User contributions licensed under CC BY-SA 3.0