Node Red and Express

4,540 views
Skip to first unread message

Stuart Feichtinger

unread,
Feb 17, 2014, 1:35:04 PM2/17/14
to node...@googlegroups.com
So I have what I am sure is a painfully simple question, but what is the easiest way to access data from Node-Red embedded in an Express framework to the app? Specifically, I am making an app that scrapes real-time bus data using embedded Node Red from my local transit authority's website and displays it on a departure board using a flat-panel tv and a Raspberry Pi running an Express framework. At the moment, I have it working, but in a very round about way (Node-Red -> MongoDB then MongoDB -> App.js). 
Long story short: If I have the data that I want and it's loaded into the Msg.payload of a flow running from Node Red embedded in an Express application, what is the best way to access the data in Msg.payload from that Express application? Or even more basically, after Red.start() in the example at http://nodered.org/docs/embedding.html, what next? Many thanks in advance!

P.S. Please don't judge my ignorance too harshly. All my coding skills are self-taught (mostly through Arduino), and I started learning web development about three weeks ago. 
Screenshot.png

Nicholas O'Leary

unread,
Feb 17, 2014, 4:05:46 PM2/17/14
to node...@googlegroups.com
Stuart,

embedding node-red in a wider express app is probably not the best route to take.

For what you want, I would recommend the following steps:

1. in settings.js, set httpStatic to point to a directory of the html/js/css/etc content you want to serve from node-red
2. set httpRoot to something like "/admin" to move the node-red ui away from '/'

At this point, when you point a web-browser at http://localhost:1880 you will get index.html served out of wherever httpStatic points at. Next, to get events flowing from the node-red flows into the webpage. For that, we can use websockets.

3. use a websocket output node in node-red - send any updates you want in the webpage to that websocket node
4. create a websocket connection from the webpage back to wherever your websocket output node is listening.

I realise that a bit of a high-level fly past, but does it make sense? 

Nick



--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Stuart Feichtinger

unread,
Feb 20, 2014, 2:26:10 PM2/20/14
to node...@googlegroups.com
Nick,
Thanks for getting back to me so quickly. That does make a lot of sense and I will give it a try. I really appreciate the help. There are a lot of great resources on the web to learn this stuff, but sometimes when it comes down to specifics, it's just really nice to be able to talk to someone who knows what they're doing. Thanks again!

Stuart

P.S. I have one last question if I may. Is using socket.io on the webpage backend to interface with Node-Red barking up the wrong tree? I realize that Node-Red uses websockets, but most of the tutorials I can find deal with socket.io. If you can even just point me in the right direction, I'd greatly appreciate it!

Dave C-J

unread,
Feb 20, 2014, 3:03:46 PM2/20/14
to node...@googlegroups.com
Hi Stuart

here is a simple web page I use to show how to connect and display very simple payloads - it's connecting to an endpoint in Node-RED at admin/ws/myapp
It also shows the connected status of the socket, and retries to connect every second.

<code>

<!DOCTYPE HTML>
<html>
    <head>
        <title>Simple Display</title>
    </head>
    <script type="text/javascript">
        var server = window.location.hostname;
        var topics = {};
        var wsUriC = "ws://"+server+":1880/admin/ws/myapp";
        function wsConnectC() {
            console.log("connect",wsUriC);
            var ws = new WebSocket(wsUriC);
            ws.onmessage = function(msg) {
                var line = "";
                console.log(msg.data);
                line += "<p>"+msg.data+"</p>";
                document.getElementById('messages').innerHTML = line;
            }
            ws.onopen = function() {
                document.getElementById('status').innerHTML = "connected";
                console.log("connected");
            }
            ws.onclose = function() {
                document.getElementById('status').innerHTML = "not connected";
                setTimeout(wsConnectC,1000);
            }
        }
    </script>
    <body onload="wsConnectC();" onunload="ws.disconnect;">
        <h1><a href="scroll.html">Temperature Display</a></h1>
        <div id="messages"></div>
        <hr/>
        <div id="status">unknown</div>
    </body>
</html>


</code>
--
regards

Dave Conway-Jones

Dave C-J

unread,
Feb 20, 2014, 3:04:43 PM2/20/14
to node...@googlegroups.com
PS - I should have said - this assumes a browser that has built in websocket support like recent versions of Firefox or Chrome etc....
--
regards

Dave Conway-Jones

Stuart Feichtinger

unread,
Feb 20, 2014, 3:26:37 PM2/20/14
to node...@googlegroups.com
Dave,
Thank you very much! That is really helpful and I can't wait to get home and try. I'm sorry for all the questions, but I have another if you'll indulge ms. I'm still getting used to client-side vs server-side stuff and what to do where. 

My setup: I'm using express to run the application backend (an airport-style departure board for the local bus system at my school). I'm using Node-Red to scrape the live bus times from the metro website and an Express app to display them. This website will be served from and displayed by a Raspberry Pi in kiosk mode attached to a tv.

Would you suggest using the express-based node application to access the data from Node-Red through the websocket and render the appropriate page (using Jade) with the data pre-populated (server-side?) or bypass the application and embed it straight in the web page (client-side?) like, I think, in your example? I hope I didn't butcher that too badly and that it all makes sense Thank you again! I really appreciate the help.

Stuart

Nicholas O'Leary

unread,
Feb 20, 2014, 5:37:57 PM2/20/14
to node...@googlegroups.com
Stuart,

personally, given the convenience of serving static content out of node-red, I would do the data rendering on the client side using the websocket technique we've described. In other words, not bother with a separate express app. That assumes there aren't specific reasons you need to do the rendering on the server side.

Nick




Message has been deleted

Andrew Jawitz

unread,
Mar 19, 2014, 11:19:09 AM3/19/14
to node...@googlegroups.com

 I am making an app that scrapes real-time bus data using embedded Node Red from my local transit authority's website and displays it on a departure board using a flat-panel tv and a Raspberry Pi

Stuart,
  This sounds like an awesome project!  Do you have any other documentation on how it turned out? It seems that most real-time transit interfaces were created around 2011-2012 and didn't have options like we have today. The only other exception I can think of is Brendan Nee's Node-GTFS library. So I'm really happy to see somebody else taking this approach!  

Sanjana H E

unread,
Nov 30, 2016, 7:06:23 AM11/30/16
to Node-RED
hi can i know how to embed node-red in existing node app. plz reply ASAP

Nicholas O'Leary

unread,
Nov 30, 2016, 7:07:43 AM11/30/16
to Node-RED Mailing List
Hi,

Have you read the link included in the post you've replied to? 


I realise there's not a lot to go on there, but it is a starting point.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack

---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/1061931f-ecae-4c96-a215-712366645f57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sanjana H E

unread,
Nov 30, 2016, 7:17:20 AM11/30/16
to Node-RED
I have a MEAN app already running..I just want to initiate node-red along with MEAN app..i also want node-red UI on my existing Dashboard.


On Wednesday, November 30, 2016 at 5:37:43 PM UTC+5:30, Nick O'Leary wrote:
Hi,

Have you read the link included in the post you've replied to? 


I realise there's not a lot to go on there, but it is a starting point.

Nick
On 30 November 2016 at 12:06, Sanjana H E <sanja...@gmail.com> wrote:
hi can i know how to embed node-red in existing node app. plz reply ASAP

On Tuesday, February 18, 2014 at 12:05:04 AM UTC+5:30, Stuart Feichtinger wrote:
So I have what I am sure is a painfully simple question, but what is the easiest way to access data from Node-Red embedded in an Express framework to the app? Specifically, I am making an app that scrapes real-time bus data using embedded Node Red from my local transit authority's website and displays it on a departure board using a flat-panel tv and a Raspberry Pi running an Express framework. At the moment, I have it working, but in a very round about way (Node-Red -> MongoDB then MongoDB -> App.js). 
Long story short: If I have the data that I want and it's loaded into the Msg.payload of a flow running from Node Red embedded in an Express application, what is the best way to access the data in Msg.payload from that Express application? Or even more basically, after Red.start() in the example at http://nodered.org/docs/embedding.html, what next? Many thanks in advance!

P.S. Please don't judge my ignorance too harshly. All my coding skills are self-taught (mostly through Arduino), and I started learning web development about three weeks ago. 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack

---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Ben Hardill

unread,
Nov 30, 2016, 7:53:35 AM11/30/16
to Node-RED
http://stackoverflow.com/questions/40858962/integrating-node-red-in-mean-js-existing-dashboard/40861882#40861882

This is the 3rd time this has been asked this week.

I'm beginning to think this is part of an assignment from somewhere

Julian Knight

unread,
Nov 30, 2016, 12:53:09 PM11/30/16
to Node-RED
ou might find this of small help: https://github.com/TotallyInformation/node-red-template-embedded

Doesn't really expand on the Express side but it is a valid starting point.

Sebastian Barwe

unread,
Dec 1, 2016, 6:33:30 AM12/1/16
to Node-RED
Hey Juian,

great starting point ...
what's up with the first point of the todo list ? :) would be great ... If I remind correctly you already did some work on self-signed certificates for this part ... 

Julian Knight

unread,
Dec 1, 2016, 8:33:13 AM12/1/16
to Node-RED
Haha, well caught. I guess this is because I no longer use that template myself! As NR has moved on, I no longer find it necessary for most use. If I want multiple instances, I use Nick's idea that is listed just above the Todo section.

You are correct that I recently wrote a script to generate the various certificates and keys needed to successfully use self-signed certificates with Node.JS and Node-RED. https://github.com/TotallyInformation/SelfSigned-Cert-Creator

I at least need to update the readme to reference that. Thanks. If you see an updated version number, it will only be the readme I'm afraid. I don't currently have time to update the whole thing. Happy to take pull requests though ;-)

steve rickus

unread,
Dec 1, 2016, 9:56:22 AM12/1/16
to Node-RED
I've been struggling with this for a while now -- how to easily switch between the 5 different projects I have set up. I already have a separate userdir for each project, and each one has a different set of dependencies, so as I add nodes I use --save to add them to each project's package.json file

Generally, I'd like to run the latest version of node-red and its nodes on all projects, so right now I have to npm update each project when a new version comes out. Each userdir also uses its own setting file, so if i make a change in one I generally need to make the same change in all my projects. Not a show stopper, but it gets old.

Each project has its own port number, so I can run them at the same time, if needed. If I use Nick's package template as you mentioned, would it have a separate "script" entry for each project, like this?

{
    "name"         : "node-red-projects",
    "version"      : "0.0.1",
    "dependencies": {
        "node-red": "^0.15",
    },
    "scripts": {
      "project1": "node node_modules/node-red/red.js --userDir ./proj1",
      "project2": "node node_modules/node-red/red.js --userDir ./proj2",
      "project3": "node node_modules/node-red/red.js --userDir ./proj3"
    },
}

Ideally, I would have only one port available for all my node-red projects, followed by the project name as part of the admin/ui urls:

So, I could go to localhost:1880/proj1/admin instead of localhost:18801/admin for instance...

But it seems I would have to have control of the Express server for those routes to be enabled. Julian, is that why you created the multi project template to solve that problem? In general, and assuming the same security levels for all, how do others handle bouncing between projects like this?
--
Steve

Julian Knight

unread,
Dec 1, 2016, 2:00:54 PM12/1/16
to Node-RED
I think I would stick with a separate project folder for each and keep the same package.json in each. In other words, each project has a package.json that includes Node-RED and any other dependencies you need. Although that makes for a larger installation on disk, it means that you can optionally upgrade each project separately, great if you need to hold back the Node-RED (or other dependency) for one project. It also means that it is really easy to create a backup of each project when you are upgrading. 

So that way, you get the full benefits of my original project without the need to maintain the embedded startup.

It does mean that you need different ports if you ever want to run the projects together but you can easily do that by making sure that Node-RED takes account of an environment variable when starting. You can then pass the variable direct to the startup, something like:

> myPort=123456; npm start

and in settings.js, something like:

uiPort: process.env.myPort || 1880

You could even add some "smarts" to the iuPort setting to make it look for a free port if you wanted to.
Reply all
Reply to author
Forward
0 new messages