Building your first flow
Last updated
Last updated
Tekos flow and Node-RED share the same core, which allows you to wire together nodes to create flows which carry out your programming task. Messages pass between nodes, moving from input nodes through processing nodes to output nodes. Let’s take a brief look at nodes, flows and messages.
There are three main types of nodes:
Input Nodes (e.g. inject)
Output Nodes (e.g. debug)
Processing Nodes (e.g. function)
Input nodes allow you to input data into a Tekos-Flow application or “flow”. They have at least one output endpoint represented by the small grey square only on their right side. You use input nodes to connect data from other services, for example the Twitter, Google, serial, websockets or tcp nodes, or to manually input data into a flow using the inject node.
Output nodes allow you to send data outside of a Tekos-Flow. They have a single input endpoint on their left side. You use output nodes to send data to other services, for example via Twitter, tcp, serial or email nodes, or to use the debug node to output to the debug pane.
Processing nodes allow you to process data. They have an input endpoint and one or more output endpoints. They allow you to transform the data type (e.g. json, csv, xml) nodes, use the data to trigger a message (e.g. trigger, delay) nodes and to write custom code that uses the data received (e.g. function node).
Note that some nodes, like the inject and debug messages, have a button that allows you to actuate a node (in the case of the inject node) or to enable and disable a node (in the case of the debug node).
Flows consist of multiple nodes wired together, with output tabs linked to input tabs of the next node in the flow. Messages flow along the nodes carrying data from node to node.
Tekos-Flow nodes consume input messages and produce output messages. Messages are JavaScript objects that contain at least a “payload” parameter, like this:
Nodes consume and produce messages, usually using msg.payload as the main placeholder for the data they consume and produce. However, messages can be extended to contain other parameters. For example, to set the topic of a message and add a new parameter, location, you could create a new msg object as shown in listing 2.2.
Let’s use this knowledge to create your first flow.
To create a simple flow you start by dragging an injection node from the pallet to the workspace this node allows you manually inject messages into a flow, next add a debug node and wire them together by clicking on the inject nodes output and dragging on the debug node.
With them wired together whenever the inject node sends a message it will be recived by te debug node, but at this point the flow only exist in the editor for it to starty doing anything it must be deployed to the runtime which you can do by clicking the deploy button in the header.
Once deployed you can click the button next to the inject node to trigger it, opening up the debug node sidebar shows the messages that arrive at the debug node. The default configuration of the inject node is to send the current timestamp formatted as milliseconds since January the first 1970. The default configuration of the debug node is to display the payload of each message.
Next step is to add a function by dragging it from the palette and dropping it onto the wire between the existing nodes, this splices the node into the wire.
The function node lets you write some JavaScript code that gets run against each message passing throught. To configure the node double-click on it to open it’s edit dialog, then start editing.
In this example we’ll change the code to reformat the inject nodes timestamp to a more human readable format.
Code:
With that change made and deployed, now click the inject node and you can see the formatted message arrived in debug.