Interaction between node-red and nodejs app

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 and i'am stuck on how to use data sent from my flow to my nodejs app. !!
Thanks in advance.

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.

1 Like

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')

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?

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

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 ?

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

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

Have you installed an MQTT broker, such as mosquitto?

yes i did.

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

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?

i have installed this one


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.

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.

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.

1 Like

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.

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

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

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

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.

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