# JavaScript equivalent of node-daemon?

**URL:** https://discourse.nodered.org/t/javascript-equivalent-of-node-daemon/9879
**Category:** General
**Created:** [7 April 2019 22:46 UTC](https://discourse.nodered.org/t/javascript-equivalent-of-node-daemon/9879 "2019-04-07T22:46:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![SirHumphreyAppleby](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@SirHumphreyAppleby](https://discourse.nodered.org/u/SirHumphreyAppleby)
#### Post date: [7 April 2019 22:46 UTC](https://discourse.nodered.org/t/javascript-equivalent-of-node-daemon/9879/1 "2019-04-07T22:46:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [8 April 2019 03:53 UTC](https://discourse.nodered.org/t/javascript-equivalent-of-node-daemon/9879/2 "2019-04-08T03:53:24Z")

</div>

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

---

<div class="post-metadata">

### Author: ![SirHumphreyAppleby](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@SirHumphreyAppleby](https://discourse.nodered.org/u/SirHumphreyAppleby)
#### Post date: [8 April 2019 04:49 UTC](https://discourse.nodered.org/t/javascript-equivalent-of-node-daemon/9879/3 "2019-04-08T04:49:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [8 April 2019 07:01 UTC](https://discourse.nodered.org/t/javascript-equivalent-of-node-daemon/9879/4 "2019-04-08T07:01:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![SirHumphreyAppleby](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@SirHumphreyAppleby](https://discourse.nodered.org/u/SirHumphreyAppleby)
#### Post date: [8 April 2019 07:29 UTC](https://discourse.nodered.org/t/javascript-equivalent-of-node-daemon/9879/5 "2019-04-08T07:29:55Z")

</div>

Thank you. That makes the code a lot tidier.
