Detailed explanation of writing API for creating custom node JavaScript files in Node-RED

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Detailed explanation of writing API for creating custom node JavaScript files in Node-RED

Introduction

In Node-RED, if you cannot find the node you need, you can customize a node to meet your requirements. In previous articles, I briefly introduced how to create a node, using case conversion as an example. Although the example is simple, it allows you to understand the steps for creating a custom node and the components of a node. This article will focus on the JavaScript file of a custom node, explaining in detail the APIs and other configuration items. As we know from previous articles, the JavaScript file of a node is the core file of a node, used to receive configuration parameters, process business logic, and send data downstream. Let's take a look together.

Custom Node JavaScript File

Node Builder

All nodes are generated by a builder function, which creates a new instance. This function is registered in the runtime,
and exists in the flow editor by setting a configuration object.
In this function, the first thing to do is call theRED.nodes.createNodefunction to serialize the properties, which are shared by other nodes. After that,

you can write your business code.
As follows

Receiving Information

The node is registered by ainputEvents are used to receive information sent by upstream nodes throughout the flow. When registering an event, there are two parameters in the event callback function: msg and send. msg is the data passed from upstream. send is a function to be issued downstream. The third function, done, is called after the node completes the business logic processing.
Example

function SampleNode(config) {
    RED.nodes.createNode(this,config);
    // node-specific code goes here

}

RED.nodes.registerType("sample",SampleNode);

Receiving Information

A node receives information sent by upstream nodes throughout the flow by registering ainputevent. When registering an event, there are two parameters in the event callback function: msg and send. msg is the data passed from upstream. send is a function to be issued downstream. The third function, done, is called after the node completes the business logic processing.
Example

this.on('input', function(msg, send, done) {
    // do something with 'msg'

    // Once finished, call 'done'.
    // This call is wrapped in a check that 'done' exists
    // so the node will work in earlier versions of Node-RED (<1.0)
    if (done) {
        done();
    }
});

Sending Information

Sending information downstream in a custom node is very simple
by simply using the send function, as shown below

var msg = { payload:"hi" }
this.send(msg);

If a node wants to send information downstream in the input event, it can be done like this

let node = this;
this.on('input', function(msg, send, done) {
    // For maximum backwards compatibility, check that send exists.
    // If this node is installed in Node-RED 0.x, it will need to
    // fallback to using `node.send`
    send = send || function() { node.send.apply(node,arguments) }

    msg.payload = "hi";
    send(msg);

    if (done) {
        done();
    }
});

In the callback function of the input event, there are three parameters: the first is the received information, the second is the send function,
and the third is the done function. Each has different usage scenarios.

If multiple parameters are to be input downstream, they can be written as follows:

this.send([ msg1 , msg2 ]);

this.send([ [msgA1 , msgA2 , msgA3] , msg2 ]);

Diary event

To print logs in a custom node, simply call methods such as log and warn under this. The logs will be printed in the console.

this.log("Something happened");
this.warn("Something happened you should know about");
this.error("Oh no, something bad happened");

// Since Node-RED 0.17
this.trace("Log some internal detail not needed for normal operation");
this.debug("Log something more details for debugging the node's behaviour");

Set status

Some nodes have a status display to indicate whether they have been successfully configured or if there is a configuration error.
This status is also set in the js file, and you only need to call status.
Below are the options:

this.status({fill:"red",shape:"ring",text:"disconnected"});

fill for the color of the status
shape for the icon of the status
text for the text of the status

Custom node settings

If you want to configure a property of a node in the setting file when creating the node, you can do so as follows.
Assuming your node type isfizz-node, and you placecolorin the setting file, you need to configurefizzNodeColor.
in the setting file. When using it, useRED.settings.fizzNodeColorto access it.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.