Implementing call and return

I am developing an application with several stepper motors on Raspberry Pi. Each stepper motor has its own sub-flow with inject node to update its state and handle commands (home, move, reset). Various nodes can use link out/in to send a msg COMMAND to a stepper sub-flow. The question is how to best receive a response that the command is complete. In other languages, one would just use call and return.

  1. Use link out/in to connect the msg output of the motor sub-flow to all interested nodes. These nodes will need to screen out messages not relevant to their request, perhaps by adding a field to the original message (e.g. msg.source). I do see that there are contributed flows to provide subroutine like features (action flows and components), so that is an option.
  2. Have the motor sub-flow set a global variable with its status and then the "calling" function node can poll this variable. This will require a bit of handshaking since the calling node cannot be sure when the sub-flow has received the command.
  3. Ditch the messages and have the calling node set a global variable with the command and then poll a global variable that is set by the motor sub-flow. The polling can be triggered using setInterval or an inject node. While this seems un-NodeRed like, there are built-in nodes that are essentially doing just that (e.g. digital in, watch, serial in).

Opinions? Other options?

Thanks,
Tom

The JavaScript way would most likely be to implement an event handler. Then you set up a function that is run whenever a specific event is received.

Not sure if there are any existing nodes that will do that. However, you can do it yourself using function nodes and Node.js's event library. You may even be able to use Node-RED's event object but I'm not sure if it is exposed to function nodes? If you use your own event object, you need a flow that sets it up and saves it to a global or flow variable (has to be an in-memory var so you need to do the setup every time Node-RED starts). Then, when you want to use it in a function node, you grab the variable and create a listener function on it the fires out a msg whenever the event fires.

I'm already doing something similar to that with my Drayton Wiser node.js module - not enough time (or too lazy!) to set up a full node-red set of custom nodes and the module creates some consumable events, works well.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.