DEV Community

Cover image for Learning Node-RED Through 6 Practical Labs
Ibrahim abo eita
Ibrahim abo eita

Posted on

Learning Node-RED Through 6 Practical Labs

A collection of 6 Node-RED lab flows for learning the basics: Hello World with Inject/Debug, custom logic with Function nodes, no-code routing with Switch/Change, context exchange across Node/Flow/Global scopes, and MQTT pub-sub with a live dashboard via @flowfuse/node-red-dashboard.

Lab 1: Hello World

*Nodes Used: [Inject - Debug]
*

FLOW: core message flow between 2 nodes with an inject node to reserve the message and debug node to
print the payload.

  • Inject Node typically used to inject data, start the flow, testing, or periodic cycles
  • Debug Node inspects the data flowing through the wires (connections between nodes) and is essential for troubleshooting.

Lab 2 - Function Nodes

*Nodes Used: [Inject - Function - Debug]
*

FLOW: same concept as lab 1 but with an additional Function node to process the data passed from the
previous node into different output.

msg.payload = msg.payload + " - processed by Function node";
msg.timestamp = new Date().toISOString();
return msg;
Enter fullscreen mode Exit fullscreen mode
  • Function Node: Runs custom JavaScript code on the incoming message.

Lab 3 - Switch

*Nodes Used [Inject - Change - Switch - Debug * 2]
*

FLOW: The key idea is to control the flow with no code as we convert the degrees from
celsius to fahrenheit and routing the output to different slots based on the value.

  • Change Node: Runs on the main event loop of Node-RED,while Function Node is sandboxed JS Execution. Modifies message properties without writing code. Can set, change, move, or delete properties.
  • Switch Node: Routes a message to different outputs based on rules.

Lab 4 - Context Exchange

*Nodes Used [Inject * 2 - Change * 2 - Debug]
*

FLOW: Node-RED has three scopes of context:
Scope Visibility Typical use
Node Only the single node that set it Private counters, local state inside one
Function node
Flow All nodes on the same tab/flow Shared data for one workflow
Global Entire Node-RED instance Data needed by multiple flows
Context lets you keep data between messages or across different parts of a flow.

Lab 5 - MQTT

*Nodes Used [Inject * 2 - MQTT IN - MQTT OUT - Change - Debug]
*

FLOW: Decoupled communication.

  • mqtt out node: Publishes a message to a topic on a broker.
  • mqtt in node: Subscribes to a topic and injects received messages into the flow.

Lab 6 - MQTT

*Nodes Used [Inject * 2 - MQTT IN - MQTT OUT - Change - Debug]
*

FLOW: Installing the @flowfuse/node-red-dashboard palette adds UI nodes. The
dashboard is served at http://localhost:1880/dashboard.

msg.payload = new Date(msg.payload).toLocaleString();
return msg;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)