JavaScript equivalent of node-daemon?

#1

Is there an equivalent to node-daemon that allows a persistent JavaScript function, where messages to be read and processed in a loop?

Looking at the daemon source, it appears the input functionality is provided by node.on("input", function), however using this in a function results in... "Error: Cannot add listener to 'input' event within Function". That's not entirely unexpected.

I am wanting to get and set raw GPIO from remote MQTT clients which have no internal state of their own. Node-RED would be responding to binary input and time input, creating its own state.

0 Likes

#2

The way you describe it, this is exactly what node-red is and does.

0 Likes

#3

I understand Node-RED is calling the JavaScript defined in the function node when a message comes in. The node-daemon node appears to allow a persistent process to handle messages via stdin/stdout/stderr. The called process is able to maintain state internally. I am wanting to know if there is an equivalent 'function' node that allows the JavaScript to handle multiple messages, rather than being called for each incoming message, and if that functionality is there, I'd appreciate a link indicating how to use it.

I am hoping avoid having to call flow.get/flow.set each time the function is called, and relying on inject nodes to provide a 'tick' to progress states after a period of time.

0 Likes

#4

The code you write for a function node is already inside the "on input" call. Inside a function node you can just use local in memory context eg. context.myvar rather than set get.

0 Likes

#5

Thank you. That makes the code a lot tidier.

0 Likes