# Interaction between node-red and nodejs app

**URL:** <https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656>\
**Category:** General\
**Created:** [29 May 2019 08:45 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656 "2019-05-29T08:45:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [29 May 2019 08:45 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/1 "2019-05-29T08:45:40Z")

</div>

i have a NR flow contains an s7 node. Any tips to guide me how to display data sent from my flow to nodejs app ?  
i have tried this example [https://nodered.org/docs/embedding](https://nodered.org/docs/embedding) and i'am stuck on how to use data sent from my flow to my nodejs app. !!  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 May 2019 19:55 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/2 "2019-05-29T19:55:32Z")

</div>

There are all sorts of ways to achieve this. You don't really need to embed unless you want to do something clever with the ExpressJS server.

Assuming that your app is on the same device as Node-RED, you can use pretty much any of the approaches: udp, tcp, websockets. Possibly even native unix pipes.

Of these, unix pipes would give the highest performance but I'm not really familiar with using them. UDP has the next lowest overhead but that will really only work across a single local secure network.

If you use UDP or TCP, Node.JS has native libraries to support these so connecting a udp-out node in Node-RED to a UDP network connection in a Node.js app is pretty straight-forwards. Similarly with TCP. Websockets is generally best left to communications with a browser.

You could also use a message broker to act as an intermediate. This is likely to be a better approach if you might need to manage multiple endpoints in the future. MQTT is really lightweight and easy to set up. There are libraries for your own app. In Node-RED, you would use an mqtt-out node and publish data to one or more "topics". In your app you would configure the MQTT library to listen on those topics.

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [30 May 2019 10:08 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/3 "2019-05-30T10:08:24Z")

</div>

Thanks alot for the informations.  
i have tried with MQTT. have put this code, found it in demo, on my nodejs app

> var mqtt = require('mqtt')  
> var client = mqtt.connect('[http://127.0.0.1:1880/#flow/7f69cb3a.efc464](http://127.0.0.1:1880/#flow/7f69cb3a.efc464)')
> 
> client.on('connect', function () {  
> client.subscribe('presence', function (err) {  
> if (!err) {  
> client.publish('presence', 'Hello mqtt')  
> }  
> })  
> })
> 
> client.on('message', function (topic, message) {  
> // message is Buffer  
> console.log(message.toString())  
> client.end()  
> })

and i put an MQTT in node. i have set client "localhost:5000" but it doesn't establishe connection between the two apps!!  
am'i wrong in something?

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [30 May 2019 12:33 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/4 "2019-05-30T12:33:32Z")

</div>

here's another example with TCP , and doesn't work..  
i have set nodejs as a client

```auto
var net = require('net');

var client = new net.Socket();
client.connect({
  port:1880
});

client.on('connect',function(){
  console.log('Client: connection established with server');

  console.log('---------client details -----------------');
  var address = client.address();
  var port = address.port;
  var family = address.family;
  var ipaddr = address.address;
  console.log('Client is listening at port' + port);
  console.log('Client ip :' + ipaddr);
  console.log('Client is IP4/IP6 : ' + family);

  // writing data to server
  client.write('hello from client');

});

```

but server, wich is Node-red, respond with :  
Data from server:HTTP/1.1 400 Bad Request

i have notice in debug console that client does not listen on 1880 port  
---------client details -----------------  
Client is listening at port60468  
Client ip :127.0.0.1  
Client is IP4/IP6 : IPv4

Any tips ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2019 13:03 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/5 "2019-05-30T13:03:14Z")

</div>

In the mqtt node in node red is it showing connected?

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [30 May 2019 13:06 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/6 "2019-05-30T13:06:35Z")

</div>

no it doesn't. it stays in "connecting" mode.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2019 13:12 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/7 "2019-05-30T13:12:07Z")

</div>

Have you installed an MQTT broker, such as mosquitto?

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [30 May 2019 13:12 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/8 "2019-05-30T13:12:57Z")

</div>

yes i did.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2019 13:13 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/9 "2019-05-30T13:13:45Z")

</div>

Stop and restart node red and post the startup log here, starting with the welcome to node red message.

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [30 May 2019 13:23 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/10 "2019-05-30T13:23:02Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a7d6b121041242a799431979fb12efcdffdf3097.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2019 14:34 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/11 "2019-05-30T14:34:32Z")

</div>

I see you have configured the MQTT node to connect on port 5000, is that the port you have configured in the mqtt broker? The usual port for mqtt is 1883.

What mqtt broker have you installed?

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [30 May 2019 16:25 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/12 "2019-05-30T16:25:56Z")

</div>

i have installed this one

> **[node-red-contrib-mqtt-broker](https://flows.nodered.org/node/node-red-contrib-mqtt-broker)**
>
> MQTT Broker server on Node-RED

  
If i configure port : 5000 in mqtt broker this error shows up:  
"Error starting mosca mqtt server, cause: Error: listen EADDRINUSE: address already in use :::5000"  
because my app is already running.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2019 16:30 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/13 "2019-05-30T16:30:13Z")

</div>

What app is already running? If you stop node red and start it again then it should not be running. I have just noticed the messages about mosca in the log you posted, first it says binding on 1883, then it says it can't listen on 8080. I don't know anything about the mosca server so someone else will have to help here.  
**[Edit]** Hold on, do you mean you are trying to make your node app listen on the same port as the mqtt broker? You can't do that, it isn't how mqtt works. An mqtt client does not listen on the same port as the broker.  
**[More edits]** Concentrate on getting node red talking to the mqtt broker first, then get it going with your app.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 May 2019 19:36 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/14 "2019-05-30T19:36:42Z")

</div>

> [@hathemi](#):
>
> var client = mqtt.connect('[http://127.0.0.1:1880/#flow/7f69cb3a.efc464](http://127.0.0.1:1880/#flow/7f69cb3a.efc464)')

MQTT requires a broker which is a separate application. Try installing the Mosquitto broker. Then change that URL to `mqtt://localhost` (assuming your code and the broker are on the same device.

You can use a cross-platform GUI such as MQTT-Spy to monitor what is happening in the broker so you should be able to see your published topic appear.

Then create an MQTT connection in Node-RED to the same broker via the mqtt-in node. Then listen for the `presence` topic.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 May 2019 19:38 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/15 "2019-05-30T19:38:17Z")

</div>

> [@hathemi](#):
>
> var client = new net.Socket(); client.connect({ port:1880 });

This won't work because 1880 is the port for the Node-RED admin web server (that delivers your admin UI where you create your flows, etc.

When you add a TCP-in node, you specify a port and you use that in your code.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2019 20:11 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/16 "2019-05-30T20:11:18Z")

</div>

> [@TotallyInformation](#):
>
> MQTT requires a broker which is a separate application. Try installing the Mosquitto broker.

I believe @hathemi is using node-red-contrib-mqtt-broker, that does the same job as mosquitto doesn't it?

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [30 May 2019 23:52 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/17 "2019-05-30T23:52:48Z")

</div>

you're right, now it works perfectly  
But, my problem now is how to receive value sent from Node-red to nodejs app.??

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 May 2019 00:13 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/18 "2019-05-31T00:13:54Z")

</div>

> [@Colin](#):
>
> I believe @hathemi is using node-red-contrib-mqtt-broker, that does the same job as mosquitto doesn't it?

Not used it but the settings should be the same if everything is running on the same device.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 May 2019 08:01 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/19 "2019-05-31T08:01:25Z")

</div>

> [@hathemi](#):
>
> But, my problem now is how to receive value sent from Node-red to nodejs app.

Your need an mqtt client in your app so your first approach starting  
`var mqtt = require('mqtt')`  
should work, but of course you should be connecting to the mqtt broker (so something like localhost:1883) not to node red.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 May 2019 08:04 UTC](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656/20 "2019-05-31T08:04:04Z")

</div>

> [@Colin](#):
>
> but of course you should be connecting to the mqtt broker (so something like localhost:1883) not to node red.

I already gave what should be the right link. Shouldn't be a need to specify a port.

[Next page](https://discourse.nodered.org/t/interaction-between-node-red-and-nodejs-app/11656.md?page=2)
