Search code examples
javascriptnode.jsmongodbunixunix-socket

Processing external triggers in Node.js applications


I have a Node.js daemon application that runs on my Debian home server 24/7.

I would like it to process triggers generated by motion, a program that monitors the video signal from cameras that is installed on the same machine. Motion can execute a command on certain events, for example when motion was detected, or camera connection was lost.

I can write a script that will process these events and record them in the database and in my daemon I can continuously poll the database. But that would be highly inefficient, right?

What would be the optimal way to process external triggers in Node.js applications?


Solution

  • Have a look at dnode. It allows you to do exactly what you are looking for.

    In your daemon you will have something like this.

    var dnode = require('dnode');
    var server = dnode({
        transform : function (eventObject, cb) {
            //handle the event
    
            cb(callbackDataHere)
        }
    });
    server.listen(5004);
    

    You will then need to create the command that Motion will call

    var dnode = require('dnode');
    
    var d = dnode.connect(5004);
    d.on('remote', function (remote) {
    
        var eventDataToSend = {}
    
        remote.transform(eventDataToSend, function (s) {
            //Do stuff with arguments sent back from the callback on the server
        });
    });