How-To: Use the new node.on close and node.send functions

246 views
Skip to first unread message

Julian Knight

unread,
Apr 1, 2015, 11:49:56 AM4/1/15
to node...@googlegroups.com
Hi all, thought I'd quickly share this short how-to on using the new node.on close and node.send features in a function node.

I have a number of things that I want to set up when NR starts so I already had 3 nodes to do this:

inject (set to run once at startup) -> function (containing startup tasks) -> debug (telling me startup completed)

I now changed this to include the following in the function node:

msg.topic = "Starting";
msg.payload = "Startup completed";

node.on('close',function() {
    msg.topic = "Closing";
    msg.payload = "Closing down now";
    node.send(msg);
});

return msg;

With a further slight amendment, you can do some additional work as the system closes. Set the function to have two outputs and:

node.on('close',function() {
    msg.topic = "Closing";
    msg.payload = "Closing down now";
    node.send([null, msg]);
});

return [msg, null];

In each case, the return command triggers a message on startup and the on close triggers a message when shutting down. However, it should be noted that you are very limited as to what you can do during shutdown. For example, I tried to trigger an MQTT output node from the closing output and it doesn't execute it, a debug output node works however.

Hope this is helpful.

Julian.

Nicholas O'Leary

unread,
Apr 1, 2015, 11:55:41 AM4/1/15
to Node-RED Mailing LIst
Julian, nice approach, although it is by no means guaranteed to work, as you have already noticed. If the node you are wired to has already been closed, your message probably isn't going to get there. Nodes are closed in the order they were added to the workspace (as that is the order they appear in the flow file).... some close asynchronously, some close synchronously... so there are plenty of race conditions to overcome :)

Nick

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julian Knight

unread,
Apr 1, 2015, 12:43:32 PM4/1/15
to node...@googlegroups.com
Hi Nick, thanks. I actually didn't really start with that in mind so anything that works is a bonus! As you say though, there are too many edge cases so I wouldn't rely on downstream nodes to activate, normally I'd simple add some code to the on close function directly. But it was a useful exercise and at least illustrates how things work.

Made me think though that there might be some use cases for keeping track in MQTT of whether NR was up or dead. I guess this would be a valid use for the Last Will & Testament feature in MQTT. With NR keeping a permanent connection open to MQTT having registered the connection with a LW&T - maybe something to add to the wish list do you think? Could be very useful when NR is part of a bigger system.
Reply all
Reply to author
Forward
0 new messages