Spanish Total.js
English
Documentation For AI agents
Contents

Inputs, outputs and messages

Declare inputs and outputs, receive messages, send them on and end them.

.md GitHub
On this page

Inputs and outputs are the connection points of a node. Data travels between nodes as messages: your component receives a message on an input, and sends it on through an output or ends it.

Declare inputs and outputs

Each input and output has an id, which you use in code, and a name, which the node shows:

exports.inputs = [{ id: 'input', name: 'Text' }];
exports.outputs = [{ id: 'output', name: 'Object' }, { id: 'error', name: 'Error' }];

A component without inputs, such as a trigger, starts messages itself. A component without outputs, such as Print, ends every message it receives.

Receive messages

Flow calls instance.message for every message that arrives on any input. $.input tells you which input it came through:

instance.message = function($) {
    // $.data is the data, $.input is the id of the input
    $.send('output', $.data);
};

You can also define a handler for one input. Its name is message_ followed by the id of the input:

exports.inputs = [{ id: 'input', name: 'Input' }, { id: 'second', name: 'Second' }];

exports.make = function(instance, config) {

    instance.message_second = function($) {
        // only messages from the "second" input
        $.destroy();
    };

};

When a component defines both, Flow calls message_second first and then message for the same message, so let message skip that input:

instance.message = function($) {
    if ($.input === 'second')
        return;
    $.send('output', $.data);
};

Send a message on

$.send(output, data) sends the message to every node connected to that output. Without data, the message keeps its data:

$.send('output');                     // the same data
$.send('output', { name: 'Jane' });   // new data
$.send(null, 'to every output');      // every output that has a connection

Flow copies object data for each connection, so a node can change its copy without changing the data of the others. To turn this off for a FlowStream, see Enable cloning of message data in FlowStream fields.

End a message

A message that you do not send on must end, or the designer counts it under Pending forever. Call $.destroy() (or $.end()):

instance.message = function($) {
    console.log($.data);
    $.destroy();
};

Keep data along the way

$.repo is an object that travels with the message from node to node. A node can store a value in it, and the nodes after it can read it:

// first node
$.repo.orderid = 1001;
$.send('output');

// a node later in the flow
var orderid = $.repo.orderid;

Start a new message

A node without inputs, such as a trigger or a timer, starts its own messages with instance.send:

instance.send('output', { ts: new Date() });

See Triggers, timers and closing.

Change the outputs from the configuration

A component can build its outputs from its settings. Set instance.outputs and save the node, and the designer redraws it. The Code component creates its outputs this way:

instance.configure = function() {
    instance.outputs = [];
    for (let i = 1; i <= config.outputs; i++)
        instance.outputs.push({ id: 'output' + (i === 1 ? '' : i), name: 'Output #' + i });
    instance.save();
};

The full list of message properties and methods is in Message reference.

Made in Europe