There's a lot more to building a good Node-RED flow than just placing and wiring nodes. It's important to regularly backup
your flows, make the most of Node-RED features, and thirdly debug your programs.
These things will help keep your applications as understandable and
maintainable as possible. If you establish these good habits early on
they'll be second nature when you start working on more complex projects. In this
workshop I'll show you some tools techniques and features that make Node-RED better to work with and easy to maintain. A very important maintenance feature of
Node-RED is the ability to import and export flows not only is this a great
way to share your programs through simple text format but also a very
convenient way to backup flows in case anything happens to your system or you
want to wind back to an earlier version of your project. I'll go into Node-RED
and show you how to backup your flows now. In Node-RED
highlight the nodes you want to export come over to the menu in the top right
corner hover over export and select clipboard.
If you want to export only the highlighted nodes you can leave it on
selected nodes and click export to clipboard. Now when I go back to the menu,
hover over import, select clipboard, paste my text in the window, and when I select
a current flow and push import I can place them anywhere on the screen. Left
click to anchor them and they're now in the flow. If instead I wanted to bring these into
their own flow under import, clipboard I can choose new flow, place my text in the
window, and when I select import it'll bring it open into a new flow where I
can place them here instead. If you want to export an entire flow and not just
highlighted nodes you'll notice that the export button is grayed out until you
actually do highlight some nodes. Now that I've highlighted the nodes I can
select export, clipboard and choose to export the current flow. Now when I
choose export to clipboard and import the flow I'll find that it opens it in a
new window with the same name even though I chose to import it in the
current flow. This is because I imported an entire flow page and not just the
node formation. Highlight the nodes you want to export, come over to the menu, in
the top right corner, hover over export, and select clipboard.
Importing and exporting all flows essentially does the same thing, but
instead, it grabs every single page and imports them all at once. To move nodes between flows select them
and use either ctrl C to copy or ctrl X to cut. Move to your next flow, use ctrl V
to paste and then left-click to anchor them. iIf you want to make a new flow
without importing a new page you can use this + icon in the top right. To delete a
flow double-click the title and use the Delete key in the top left. I'll delete a
few of the flows I've made now. From that same screen you can see that you can
rename your flow and give it a brief description. Here you could include
things like flow functionality or simply who the author of it is. Now when I click
done I'll see this information appear on the right hand side. As well as
documenting in this information window you can also go over to the function
nodes section and grab a comment node even though Node-RED is a visual
programming tool comments are necessary to clarify how the flow works especially
when you're sharing the project or have multiple people working on it. When you
drag that in and edit it you can set some text in the title of the node and
go into further detail in the body. This text body supports github flavored
markdown which you can find out more about using this link here. another way to help keep things clear is
by being mindful of how you wire flows. If I want to connect to this input node
to this debug node for example having the wire go under this function node is
confusing because it looks wired in but it's actually not. If I want I can place
it in the flow by dragging it over the wire, watch it go dashed, and release. Now
it is wired into the flow. It's also unclear when nodes have the wiring
cross over each other like in this example. Here when nodes get all squished
up like this you can make the wiring a lot clearer just by spreading things out
a little. Also, when having nodes stacked like this, or just in general,
you may want to go into the settings and click 'Snap to Grid' this will make the
nodes align themselves automatically and easily. Just that makes this flow look a
lot more consistent and clear. In this settings window you can also choose to
show the grid on the screen and change the grid size. Also, in this settings window you can see
the keyboard shortcuts. This can also help improve your efficiency in Node-RED
because along with cut, copy, paste, delete, and undo you can customize a lot of
different Node-RED commands to make your programming a lot more efficient. Another
useful Node-RED feature is the built-in libraries. Just highlight the nodes that
you wish to save, go over to export, and right below clipboard there's an option
to save things to a library. You can have an organized file system using the
forward slash and give it a file name like email. Now when I export this flow
it gets saved and I can bring it into any other flow by coming into import
library, navigating through my organized structure, and grabbing the email flow.
Now I can place this on any page I'd like without the overhead of having to
use a text document. The other library function in Node-RED is for function
nodes. Here I have a simple function that splits the payload at the space
character. When I deploy and inject on this flow it'll split the string 'this is
a test' into four separate messages: this, is, a, test. If I want to reuse this code I
can go into the function node and use this book icon to save it to my library.
Again I have the option to use an organized folder structure and I can
give it a descriptive file name. Now when I save this to the library I can bring
in another function node, go to the book icon again, select open
library, go through my folder structure, and open this code and load it straight
into this node. Now when I deploy, come down and inject the flow you'll see it's
split 'hello' world' into the two messages: 'hello', 'world'. It's important to note
though that if I come down to the original node and change the split
character to be 's', click done and deploy. When I deploy an inject on this bottom
flow I'll find that 'this is a test' is broken up with each string ending in 'S'
but when I inject on 'hello world' it's still splitting at the space character
since this node is completely unchanged. If you do want to have node
functionality change in multiple places that you're using it, all from the one
source, you should use 'sub flows'. Here I'll select this one function node, go
into the menu, hover over sub flows, and click 'selection to sub flow'. Now we'll
replace both of my function nodes with this sub flow. Now when I deploy and inject both
messages they should split at the space character, which they do. If I go into the
sub flow by double-clicking it and selecting 'edit flow template' I can
change the functionality of the node here to split at the character. Oh, also,
from this screen I can rename the sub flow to be a little bit more descriptive.
I can even give it a description in the field below. now when I come back to my
original flow, deploy the changes and inject on both nodes I'll see that the
first string is split at each of the O's and the second string is not split at
all since it does not have an O character in it. The functionality has
changed in both places just from the one change. These special sub flow nodes will
appear in your node palette just like any other node and you can bring them in
as many places as you would like it. While we're here, another special node to
go over is the 'catch node'. This node just catches any error message that appears
in the debug window and triggers this node to inject. You can build up a flow
to handle whatever error comes in. I advise that you select specific nodes
for it to catch so that it only responds to errors that you have designed the
flow to handle. Since the use of this node will be very specific to the type
of errors that you're dealing with it's a bit hard to show examples. So just be
aware that it exists and try to put it into use where relevant. Often times there is more to a message
than just the msg.payload I've been seeing in the debug window. For
example, I'll bring in this 'email node' and quickly set it up. This will pull in
messages from my inbox if I bring in a default debug node and wire that into
the email node, click deploy, I'll see that when it fetches an email all I get
is the email text body. If however, I come in and edit the debug node to display
the complete message object, hit done, and deploy, and inject, I can see that there
is a message topic holding the subject msg.date holding the date
message, the header showing all sorts of other information. I can copy property
text and the path to the property using these debug buttons here. If you make use
of catch nodes, sub flows node, read libraries, and keyboard shortcuts where
appropriate and always backup your flows you will find nNode-RED to be a reliable
and efficient tool for prototyping and developing your various IoT applications.
Thanks for watching.