So far, we have learned about Node-RED using the prepared nodes. In this chapter, you'll learn how to create your own node and publish it in a library. After completing the tutorials in this chapter, you will be able to publish your own node for use by various developers around the world.
Let's get started with the following topics:
By the end of this chapter, you will have mastered how to create your own node.
To progress in this chapter, you will need the following:
Before developing a node, there is something you need to know first. The following policies are set for node development. Let's follow these and develop a node.
When creating a new node, you need to follow some general rules. They adhere to the approach adopted by the core nodes and provide a consistent user experience.
You can check the rules for creating a node on the official Node-RED website: https://nodered.org/docs/creating-nodes/.
Node-RED nodes consist of two files: a JavaScript file that defines processing and an HTML file that provides a UI such as a setting screen. In the JavaScript file, the processing of the node you create is responsible for is defined as a function. This function is passed an object that contains node-specific properties. The HTML file describes the property settings screen displayed by the Node-RED flow editor. The settings values entered on the property settings screen displayed in this HTML file are called from the JavaScript file and processed.
Here, we will create a GitHub repository, but if you just want to create a node, you don't need a GitHub repository. In this chapter, we will use the GitHub repository to publish the created node to the library, so I would like you to create the repository at the beginning of the step.
Please implement the following steps to create a GitHub repository:
Figure 13.1 – Create a repository for your own node
The repository created here exists as a project for developing nodes, and then it will be packaged and published to npm. (Of course, it is optional to publish it.)
Therefore, make sure that the repository name follows the naming convention for node development.
The GitHub repository name will be the same as the node name. In the node creation rule, the node name must be node-red-contrib-<name representing a group of nodes>, so specify the GitHub repository name accordingly. In this example, it is node-red-contrib-taiponrock.
Figure 13.2 – The repository is created as a public project
You have now created your GitHub repository.
Now let's clone the repository we just created to our local development environment by following these steps:
Figure 13.3 – Copy the URL to clone this repository
Clone the repository locally (git clone) from a command-line interface (such as a terminal) where Bash can run.
$ mkdir work
$ cd work
$ git clone https://github.com/<GitHub account>/node-red-contrib-<Any specified string>.git
$ls
node-red-contrib-<Any specified string>
Let's make a JavaScript file now.
From here, we will create the actual node processing. But don't worry, we already have the code ready. The provided code is very simple for processing the node. It's just a matter of converting the string passed as input to lowercase.
$ cd node-red-contrib-<arbitrary specified string>
module.exports = function(RED) {
function LowerCaseNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
msg.payload = msg.payload.toLowerCase();
node.send(msg);
});
}
RED.nodes.registerType("lower-case",LowerCaseNode);
}
node.js has been created.
Let's make an HTML file now.
<script type="text/javascript">
RED.nodes.registerType('lower-case',{
category: 'function',
color: '#a6bbcf',
defaults: {
name: {value:""}
},
inputs:1,
outputs:1,
icon: "file.png",
label: function() {
return this.name||"lower-case";
}
});
</script>
<script type="text/html" data-template-name="lower-case">
<div class="form-row">
<label for="node-input-name"><i class="icon- tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/html" data-help-name="lower-case">
<p>A simple node that converts the message payloads into all lower-case characters</p>
</script>
node.html has been created. This HTML file is responsible for the UI and design of the node you create. As mentioned previously, a node always consists of an HTML file and a JavaScript file.
The node implementation has been almost completed. Next, let's package the created node so that it can be deployed.
Now that we've created the node processing (JavaScript) and appearance (HTML), it's time to package it. In Node-RED, the flow editor itself is a Node.js app, and each node running on it is also a Node.js app. In other words, the packaging here is processed using npm.
We won't go into detail about npm here. If you want to know more about it, please visit the npm official website or refer to various technical articles: https://www.npmjs.com/.
Now, use the npm command to perform the following steps:
$ npm init
When you finish this step, the npm init command will generate a package.json file:
Figure 13.4 – npm init
{
"name": "node-red-contrib-<arbitrary string specified>",
"version": "1.0.0",
(abridgement)
"node-red": {
"nodes": {
"lower-case": "node.js"
}
},
(abridgement)
}
The following screenshot can be used as a reference, which will help you in adding this property:
Figure 13.5 – Edit package.json
This completes the packaging of your own node. Let's actually use this node in the next part.
You have already completed your own node. Let's add the nodes created so far to Node-RED in a local environment.
For your own nodes, it is very important to check their operation locally. Publishing a node on the internet without making sure it works in your environment is not good for many developers.
So, in this section, you'll be testing your own node in your local environment.
You can use the npm link command to test the node module locally. This allows you to develop nodes in your local directory and link them to your local Node-RED installation during development.
This is very simple. Follow these steps:
$ cd <path to node module>
$ npm link
This will create the appropriate symbolic link to the directory and Node-RED will discover the node at boot time. Simply restart Node-RED to get the changes to the node's files.
You should see that a node called lower case has been added to the function category of the palette after rebooting:
Figure 13.6 – The lower case node has been added
Figure 13.7 – Make a flow
Figure 13.8 – Result of this flow
Next, let's see how to customize a node.
I was able to confirm that the node I created can be used in the local environment. From here, we will customize that node. It is possible to edit the function and appearance of the node by modifying JavaScript and HTML. These changes will take effect when you restart Node-RED.
Currently, the node name of the created node is still a lower-case version of the sample program. Here, change this name to any name you like. Every node must have a unique name, so you should pick something that does not already exist. Follow these steps to change the name of the node:
In the example, the repository of the node is node-red-contrib-taiponrock, so change it to the taiponrock node.
This is how the package.json file looks before being modified:
Figure 13.9 – Before modifying package.json
And this is how it looks after being modified:
Figure 13.10 – After modifying package.json
For example, change lower-case to taiponrock and LowerCaseNode to TaiponrockNode.
This is how the node.js file looks before being modified:
Figure 13.11 – Before modifying node.js
This is how the node.js file looks like after being modified:
Figure 13.12 – After modifying node.js
For example, change lower-case to taiponrock.
This is how the node.html file looks before being modified:
Figure 13.13 – Before modifying node.html
This is how the node.html file looks after being modified:
Figure 13.14 – After modifying node.html
After restarting Node-RED, you can see that it has been renamed correctly:
Figure 13.15 – Your node has been renamed
Next, we will see how we can change the code of a particular node.
The main parts that implement node processing are as follows:
(abridgement)
node.on ('input', function (msg) {
msg.payload = msg.payload.toLowerCase ();
node.send (msg);
}
(abridgement)
Here, to make the work easier to understand, let's change to a method that only returns the character string of your name or nickname.
(abridgement)
node.on ('input', function (msg) {
msg.payload = "Taiponrock";
node.send (msg);
}
(abridgement)
Now let's see if it has changed. Use the flow you created earlier. The lower case node in this flow has been changed to a node whose name and processing has been changed, but it needs to be redeployed and raised. To make it easier to understand, delete the node that was once the original lower case node and relocate it.
Figure 13.16 – Replace the node you created with the renamed node and execute it
Figure 13.17 – Result of this flow
In the next section, we will see some other node customizing options that we can use.
In addition to the node name, you can customize your own node in a lot of different ways, such as node color, node icon, node category, node function, and so on. For details, please see this official document: https://nodered.org/docs/creating-nodes/appearance.
Now that we have tested and customized the node in the local environment, let's publish the node in the Node-RED library.
Here, we will publish the created node in the Node-RED library. To do that, some work is required. So far, you have created your own node and confirmed that it can be used only in your environment. However, since it is a unique node created by you, let's publish it on the internet and have everyone in the world use it. To do this, you need to publish your own node to a location called the Node-RED library, which can be found here: https://flows.nodered.org/.
Important note
The Node-RED library is a community-based place to publish nodes and flows. Therefore, you should avoid exposing incomplete or useless nodes. This is because the Node-RED users should be able to find the nodes that they want, and it is not desirable to have a mix of unwanted nodes.
So, although this chapter will explain how to publish nodes, please avoid exposing test nodes or sample node-level ones.
Follow these steps to publish your own node in the Node-RED library:
We will write the node description in the README.md file. English is the best language to write in, considering that English is a universal language.
For example, it is desirable to describe the following contents:
In this section, since it is a hands-on tutorial, only the outline and usage will be written in the README.md file. Please update README.md with the following contents:
# node-red-contrib-<Any specified string>
## Overview
This node is a node for forcibly converting all the alphabet character strings passed as input to the character string "Taipon rock".
Even if the input parameter passed is not a character string, "Taiponrock" is forcibly returned.
In this process, it is a wonderful node that changed the sample node that was executing toLowerCase, which is an instance method of String object in JavaScript, to a process that just returns a meaningless constant.
## how to use
It is used when you want to forcibly convert all the character strings of the parameters to be passed to "Taiponrock".
Figure 13.18 – Check these five files
Upload these files to the repository on GitHub. You should have done the work in the cloned repository directory, but if you are in another location, move to that repository directory. Then, execute the following command:
$ git add .
$ git commit -m "Node has been published"
$ git push
When the push finishes without error, you can see that the target file has been uploaded in the repository on GitHub:
Figure 13.19 – Your node files are uploaded
Now let's expose the node as a module. Upload the set of files using the npm command. Again, work in the cloned repository directory:
$ npm adduser
$ npm publish
You will be asked to confirm the version when you run npm publish. Don't forget to edit package.json to increase the version number, as the version must be up when you run npm publish a second time or later.
When publish is completed normally, it will be published at https://www.npmjs.com/package/node-red-contrib-<arbitrary character string>.
An example is https://www.npmjs.com/package/node-red-contrib-taiponrock:
Figure 13.20 – Your node has been published on npm
Figure 13.21 – Add your node to the Node-RED library
When the registration is complete, you can see that the created node has been added to the library:
Figure 13.22 – Your node has been published in the Node-RED library
It takes about 15 minutes for the registration of a new node. Please note that the node you registered via the Node-RED flow editor cannot be found without complete registration on the Node-RED library.
If you upgrade the version and publish it again, please refresh from your node's page of the Node-RED Library and click check for update in the Actions panel on the right side of the node screen:
Figure 13.23 – Check for the update of your node's status
Next, let's see how to delete the node published by you.
Be careful when deleting published nodes. Currently (as of October 2020), according to npm's package unpublish policy, the unpublish deadline is within 24 to 72 hours of publication. In addition, it is possible to unpublish packages that have little effect on specific conditions, such as less than 300 downloads even for 72 hours or more.
This information is expected to be updated from time to time, so please refer to the npm official website for the latest information: https://www.npmjs.com/policies/unpublish.
After unpublishing, please refresh from your node's page of the Node-RED library in the same way as when updating. Click the request refresh at the bottom of the Actions panel on the right side of the node screen.
To unpublish, execute the following command in the module directory (the directory of the cloned repository):
$ npm unpublish --force node-red-contrib- <arbitrary string>
If this command completes successfully, the module unpublishing is successful.
It is recommended that you wait at least 15 minutes after completing adding your node to the Node-RED Library.
In Node-RED of the local environment, I reflected the self-made node so that it can be used as it is. I also published it to npm for publication and registered the node in the Node-RED library. Anyone should now be able to use this node.
Here, let's try and check whether the node created this time can be installed and used without any problems from Node-RED of IBM Cloud. Please follow these steps:
Figure 13.24 – Access ing Manage palette
If the node you created is displayed in the search results, it means that it is open to the public and is the target of installation.
If it is not displayed in the search results, you must have not waited for 15 minutes after node registration. Please try again after 30 minutes or 1 hour. If you still do not find your node, there may be some other cause, so please review the procedure you have done so far and try again:
Figure 13.25 – Search for and install your node
Figure 13.26 – Make the flow
In the example, the self-made node is inserted between the flows prepared by default when the Node-RED flow editor is started for the first time.
Figure 13.27 – Result of this flow
Great job! You now know how to make your own node and publish it.
Congrats! In this chapter, you learned how to create your own node, how to customize it, and how to set it from the Node-RED library or your local machine. Creating your own node wasn't as difficult as you might think. If you create the processing content and arrange the appearance based on this procedure, you can publish your own useful node that does not already exist and have developers all over the world use it!
Also, at the end of this book, I'll give you a brief introduction to the Node-RED user community, so be sure to check that out as well.