Contexts and Globals

Estimated reading time: 4 minutes

Loopflow Function nodes support different context levels for variables. The different contexts allow you to share data among unconnected nodes and among different flows entirely.

The Three Levels

There are three levels for Function node variables that range from less visible to more visible. More visibility makes pieces of information available to more nodes and more flows.

Level 1: Context

The context level will make a variable visible to different functions within a node. The example below shows what a context level variable can do that a regular variable cannot:

  1. Connect nodes together like the picture below:

  2. Double-click the Inject node and change the payload to string. Enter The payload as your string.
  3. Double-click the Function node and enter the following lines:

    function f1 () { // Declares a function with name, f1.
        context.set("cv", " and the context variable."); // Sets our context variable, named cv, to a string message.
    }
    
    function f2 (msg) { // Declares a function named f2 that takes msg as a parmeter.
        msg.payload = msg.payload + context.get("cv"); // Adds the message in our context variable to the msg payload.
        return msg;
    }
    
    f1(); // Calls f1 to set the context variable.
    
    return f2(msg); // Returns the result of calling f2.
    
  4. Click Done. Click Save, then click the button on left side of the Inject node.

You will see the output below in the debug tab:

Meanwhile, if you try to do the same thing with a regular variable:

function f1 () {
    var contextVariable =  " and the context variable.";
}

function f2 (msg) {
    msg.payload = msg.payload + contextVariable;
    return msg;
}

f1();

return f2(msg);

You will see the below output in the debug window:

Level 2: Flow

The flow level will make a variable visible to all nodes in the flow. The example below shows how to use this context level:

  1. Connect nodes together like the picture below:

  2. Double-click the Inject node and change the payload to string. Enter The payload as your string.
  3. Double-click the upper Function node and enter the following lines:

    flow.set("fv", " here is our flow context variable."); // Set a flow context variable, named fv, to be a string message.
    msg.payload = "A variable for this whole flow: "; // Sets the msg payload to be a short string message.
    return msg;
    
  4. Click Done. Double-click the lower Function node and enter the following lines:

    msg.payload = flow.get("fv"); // Sets our message to the string stored in fv.
    return msg;
    
  5. Click Done, then click Save.
  6. Click the button on left side of the upper Inject node, then click the lower Inject node button.

You will see the output below in the debug tab:

We can use the flow context variable in any node in the flow, even if nodes are unconnected.

Level 3: Global

This global level example below will make a variable visible to all nodes in every flow that you create.

  1. Connect nodes together like the picture below:

  2. Double-click the Inject node and change the payload to string. Enter A global message! as your string.
  3. Double-click the Function node and enter the following lines:

    global.set("global_var", msg.payload); // Stores the message payload from the Inject node in a global variable named global_var.
    return msg;
    
  4. Click Done. In a new flow, connect nodes together like in step 1:

  5. Double-click the Function node and enter the following lines:

    global.set("global_var", msg.payload);
    return msg;
    
  6. Click Done. Click Save.
  7. Return to the first flow that you made and click the Inject node button.
  8. On the second flow, click the Inject node button.

You will see the output below in the debug tab:

We can use the global context variable in any node in any flow.

You can also access global context variables through Inject nodes by selecting global. from the dropdown menu.

Use Case for Global Variables

See the Flow to Save Multiple Register Values for a global variable use case.