Instance reference
The handlers, methods and properties of a node in the server code.
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.
instance.message = function($) {}| Parameter | Type | Description |
|---|---|---|
$ |
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.
instance.message = function($) {
$.send('output', String($.data).toUpperCase());
};
event
instance.message_second = function($) {}
Receives the messages of one input.
instance.message_second = function($) {}| Parameter | Type | Description |
|---|---|---|
$ |
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.
instance.configure = function() {}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.
instance.trigger = function(msg) {}| Parameter | Type | Description |
|---|---|---|
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.
instance.close = function() {}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.
instance.variables = function(variables) {}| Parameter | Type | Description |
|---|---|---|
variables |
Object |
The new variables of the FlowStream. |
event
instance.variables2 = function(variables) {}
Runs when the global variables change.
instance.variables2 = function(variables) {}| Parameter | Type | Description |
|---|---|---|
variables |
Object |
The new global variables. |
event
instance.pause = function(is) {}
Runs when the FlowStream is paused or resumed.
instance.pause = function(is) {}| Parameter | Type | Description |
|---|---|---|
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.
instance.service = function(minutes) {}| Parameter | Type | Description |
|---|---|---|
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.
instance.call = function(data, callback) {}| Parameter | Type | Description |
|---|---|---|
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.
instance.call = function(data, callback) {
callback({ id: instance.id });
};
method
instance.send(output, data)
Starts a new message on an output.
instance.send(output, data)| Parameter | Type | Description |
|---|---|---|
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.
instance.send('output', { ts: new Date() });
method
instance.newmessage(data)
Creates a new message without sending it.
instance.newmessage(data)| Parameter | Type | Description |
|---|---|---|
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.
var msg = instance.newmessage({ name: 'Jane' });
msg.send('output');
method
instance.status(data, [delay])
Sends a status to the designer.
instance.status(data, [delay])| Parameter | Type | Description |
|---|---|---|
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.
status.count++;
instance.status(status, 500);
method
instance.activity(key, name, progress)
Shows the progress of a task under Activities in the designer.
instance.activity(key, name, progress)| Parameter | Type | Description |
|---|---|---|
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.
instance.activity('import', 'Importing orders', 50);
method
instance.throw(error)
Reports an error to the designer.
instance.throw(error)| Parameter | Type | Description |
|---|---|---|
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.
instance.replace(text, [data])| Parameter | Type | Description |
|---|---|---|
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.
instance.replace('{welcome}, Jane!');
// Good morning, Jane!
method
instance.reconfigure(config)
Changes keys of the configuration from code.
instance.reconfigure(config)| Parameter | Type | Description |
|---|---|---|
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.
instance.reconfigure({ uppercase: true });
method
instance.save()
Saves the node after you changed its inputs, outputs or configuration in code.
instance.save()The designer redraws the node.
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.
instance.id
property
instance.component
The id of the component.
instance.component
property
instance.config
The configuration of the node, the same object as config in exports.make.
instance.config
property
instance.outputs
Outputs of this node that replace exports.outputs.
instance.outputsSet it and call instance.save(). instance.inputs works the same way for inputs.
property
instance.main
The FlowStream that runs the node.
instance.maininstance.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.
instance.envSee Environments.