20 API Reference
Nick O'Leary edited this page 2014-05-07 01:42:43 -07:00

This is a work-in-progress that is out of date - do not use!

Server

Nodes

Credentials

  • RED.nodes.addCredentials(id,credentials)
  • RED.nodes.getCredentials(id)
  • RED.nodes.deleteCredentials(id)

[HTTP Server](API/Server/HTTP Server)

  • RED.server.app - the Express instance serving the editor UI
  • RED.server.server - the server instance

Events

Client

Nodes

  • RED.nodes.registerType(type,node)
  • RED.library.create({})
  • RED.nodes.eachNode(cb)
  • RED.nodes.eachLink(cb)
  • RED.nodes.eachConfig(cb)
  • RED.nodes.node(id)

Validators

  • RED.validators.number()
  • RED.validators.regex(re)

UI

  • RED.view.redraw
  • RED.view.dirty(dirty)
  • RED.sidebar.addTab(title,content)
  • RED.notify(msg,type)

# RED.nodes.Node

All nodes extend this class. It handles the wiring between nodes and other things. It should not be instantiated directly, rather RED.nodes.createNode should be called from the constructor of the sub-class.

The class defines the following functions/events:

# node.send(msg)

This function causes messages to be sent on to any nodes wired to this one. A node can have multiple outputs and each output can be wired to multiple nodes.

  • If msg is undefined, no message is sent on.
  • If msg is a single message, it is sent to all nodes wired to the first output.
  • If msg is an array, each element of the array is passed to the corresponding output.
  • If any of these elements is itself an array, each element of that array is sent to the connected nodes in order.

# 'input' event

This event is emitted when the node receives an inbound message. The node should register a listener for this event in order to handle the message.

this.on("input",function(msg) {
    var msg = {topic:this.topic,payload:this.payload};
    if (msg.payload == "") { msg.payload = Date.now(); }
    this.send(msg);
});

# node.close()

This function is called when the node is being stopped, for example when a new flow configuration is deployed. This allows the node to release any resources, such as network connections, it has open.

For example, the Inject node clears the timer if one had been set:

InjectNode.prototype.close = function() {
    if (this.interval_id != null) {
        clearInterval(this.interval_id);
    }
}

# node.log(msg)

# node.warn(msg)

# node.error(msg)

Three log level functions are provided for convenience. They format the log message with the time-stamp and ID of the node. If the Debug node is active, the log messages will appear in the debug sidebar.

# RED.nodes.createNode(node,definition)

This function should be the first thing called in a node's constructor function, passing in this and the node's definition object:

function InjectNode(n) {
RED.nodes.createNode(this,n);

# RED.nodes.registerType(type,node)

After defining a node's constructor, it needs to be registered with this function. This must be done immediately after the constructor and before any additional functions are added to its prototype, for example, if the node defines its own close function.

function InjectNode(n) {
    ...
    ...
}
RED.nodes.registerType("inject",InjectNode);

# RED.library.register(type)


# RED.events

RED.events is an instance of EventEmitter. It emits the following events:

Event Description
nodes-loaded Fired when all of the node definitions have been loaded
nodes-starting Fired before the flow nodes are created
nodes-started Fired once the flow nodes have been created
nodes-stopping Fired when the flow nodes are about to be stopped
nodes-stopped Fired once the flow nodes have stopped
var events = require("../../red/red");
events.on("nodes-started",function() {
    console.log("All nodes have started");
})