Spanish Total.js
English
Documentation For AI agents
Contents

Instance reference

The handlers, methods and properties of a node in the server code.

.md GitHub
On this page

The instance is one node of the component. exports.make receives it, and you define its handlers there:

exports.make = function(instance, config) {

    instance.message = function($) {
        $.send('output');
    };

    instance.close = function() {
    };

};

Entries of type event are functions that you define and Flow calls. Entries of type method are functions that Flow provides.

API

event

instance.message = function($) {}

Receives every message that arrives on any input.
ParameterTypeDescription
$ Object The message. See Message reference.

Each message must be sent on with $.send() or ended with $.destroy(). An exception thrown here ends the message.

Forward the data in capital letters
instance.message = function($) {
	$.send('output', String($.data).toUpperCase());
};
event

instance.message_second = function($) {}

Receives the messages of one input.
ParameterTypeDescription
$ Object The message.

The name is message_ followed by the id of the input. When instance.message is also defined, Flow calls this handler first and then instance.message with the same message.

event

instance.configure = function() {}

Runs after the configuration of the node has changed.

Flow calls it when the user applies new settings and after instance.reconfigure(). It does not run when the node is created, so call it at the end of exports.make.

event

instance.trigger = function(msg) {}

Runs when the user selects the trigger button of the node.
ParameterTypeDescription
msg Object The request from the designer.

A <button name="trigger"> in the body, or exports.trigger(data) in the browser script, calls it. For a button, msg.data holds its data-data attribute. For exports.trigger(obj), msg holds the keys of obj.

event

instance.close = function() {}

Runs when the node stops.

Flow calls it when the node is removed from the design and when the component changes. Stop timers and close connections here.

event

instance.variables = function(variables) {}

Runs when the variables of the FlowStream change.
ParameterTypeDescription
variables Object The new variables of the FlowStream.
event

instance.variables2 = function(variables) {}

Runs when the global variables change.
ParameterTypeDescription
variables Object The new global variables.
event

instance.pause = function(is) {}

Runs when the FlowStream is paused or resumed.
ParameterTypeDescription
is Boolean true when paused, false when resumed.

While the FlowStream is paused, Flow delivers no messages.

event

instance.service = function(minutes) {}

Runs once a minute.
ParameterTypeDescription
minutes Number The number of minutes since the FlowStream started.
event

instance.call = function(data, callback) {}

Answers a call from the browser script of this node.
ParameterTypeDescription
data Object The data from the browser.
callback Function Call it with the answer.

The browser script calls it with exports.call(data, callback), for example to load a list for the settings form.

Answer a call
instance.call = function(data, callback) {
	callback({ id: instance.id });
};
method

instance.send(output, data)

Starts a new message on an output.
ParameterTypeDescription
output String The id of the output.
data? Object The data of the message.

Returns Number The number of nodes that received the message.

Use it in components that start messages, such as triggers and timers. Without output, the message goes to every output that has a connection.

Send the current date
instance.send('output', { ts: new Date() });
method

instance.newmessage(data)

Creates a new message without sending it.
ParameterTypeDescription
data? Object The data of the message.

Returns Object The message.

Send it later with msg.send(output). The Route component creates a message for each HTTP request this way and stores the request in msg.refs.

Create and send a message
var msg = instance.newmessage({ name: 'Jane' });
msg.send('output');
method

instance.status(data, [delay])

Sends a status to the designer.
ParameterTypeDescription
data Object Any data, for example { count: 12 }.
delay? Number Waits this many milliseconds and then sends the last status, so that many quick calls send one status.

The designer keeps the last status of each node and shows it through $STATUS in the body. See Status and the look of the node.

Count messages
status.count++;
instance.status(status, 500);
method

instance.activity(key, name, progress)

Shows the progress of a task under Activities in the designer.
ParameterTypeDescription
key String An id of the task.
name String The text of the line.
progress? Number The progress from 0 to 100.

The line disappears when progress is 100 or name is empty.

Show progress
instance.activity('import', 'Importing orders', 50);
method

instance.throw(error)

Reports an error to the designer.
ParameterTypeDescription
error String The error message.

The FlowStream keeps its last 10 errors, and the designer shows their number under Errors. See Errors and logging.

method

instance.replace(text, [data])

Fills in the variables of a text.
ParameterTypeDescription
text String The text with keys in braces.
data? Object Values for keys that are not variables.

Returns String The text with the values.

Replaces each {key} with the FlowStream variable, the global variable or the secret of that name, {hostname} with the address of the FlowStream, and other keys from data. Keys that are not found stay as they are. See Variables in components.

Fill in a variable
instance.replace('{welcome}, Jane!');
// Good morning, Jane!
method

instance.reconfigure(config)

Changes keys of the configuration from code.
ParameterTypeDescription
config Object The keys to change.

Merges the keys into the configuration, calls instance.configure, sends the configuration to the designer and saves the FlowStream.

Turn on a setting
instance.reconfigure({ uppercase: true });
method

instance.save()

Saves the node after you changed its inputs, outputs or configuration in code.

The designer redraws the node.

Two outputs from the settings
instance.outputs = [{ id: 'output', name: 'Output #1' }, { id: 'output2', name: 'Output #2' }];
instance.save();
property

instance.id

The id of the node in the design.
property

instance.component

The id of the component.
property

instance.config

The configuration of the node, the same object as config in exports.make.
property

instance.outputs

Outputs of this node that replace exports.outputs.

Set it and call instance.save(). instance.inputs works the same way for inputs.

property

instance.main

The FlowStream that runs the node.

instance.main.id is its id, instance.main.variables its variables and instance.main.variables2 the global variables.

property

instance.env

The environment of Flow: dev, test or prod.
Made in Europe