Search code examples
node.jsnode-red

How to check the global file or directory against all deployed nodes?


I'm working on an IoT application, where I have a node that keeps a list of what devices are connected to it that updates when a new message arrives.

For now I'm using the context to save the data, which is wiped on restart.

Using the node's id, I could save the list on a global JSON file, or have a file per node, but I run into a wall when it comes to maintenance.

Whenever I delete a node, its info is now trash. Is there a better way than to just check the global file or directory against all deployed nodes and delete what I don't need? And if there isn't, how do I get all current nodes?


Solution

  • Nodes have a on close callback which you could use to clean up when a node is deleted. Details can be found here

    There are 2 versions of the callback, one that handles async actions and one that doesn't.

    this.on('close', function(done) {
        doSomethingWithACallback(function() {
            done();
        });
    });
    

    and

    this.on('close', function() {
        // tidy up any state
    });