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