Node-RED: Tutorial & Best Practices

What is Node-RED?

Node-RED is a powerful flow-based development tool for visual programming. It’s designed for wiring together hardware devices, APIs, and online services, making it a fantastic choice for IoT applications. The best part? It runs on a shell, so no GUI needed!

Installing Node-RED

Node-RED might not be installed by default on your Linux server. Here’s a straightforward way to get it up and running:

  1. Update your package lists:

    sudo apt update

  2. Install Node.js and npm:

    sudo apt install nodejs npm

  3. Install Node-RED globally:

    sudo npm install -g --unsafe-perm node-red

  4. Start Node-RED:

    node-red

Basic Configuration

After starting Node-RED, you can access the editor by navigating to http://<your-server-ip>:1880 in your web browser. Here are some best practices to keep in mind:

  • Secure Your Editor: By default, Node-RED doesn’t come with authentication. To secure it, edit the settings.js file typically found in the ~/.node-red directory. Enable adminAuth and set up a username and a hashed password.

  • Use Projects for Version Control: Enable the Projects feature in the settings.js file to keep your flows under version control using Git.

Common Problems and Troubleshooting

  • High CPU Usage: If you notice high load, it could be due to inefficient flows. Use the top command to identify if Node-RED is the culprit and optimize your flows.

  • Port Conflicts: If Node-RED fails to start, check if another service is using port 1880. You can change the port in the settings.js file.

    httpAdminRoot: '/admin', uiPort: process.env.PORT || 1880,

  • Network Failure: If you can't access the editor, there might be a network failure. Ensure that your server's IP is reachable and that firewall settings allow connections on port 1880.

Best Practices

  • Modularize Flows: Break down complex flows into smaller, reusable sub-flows. This makes your workflow cleaner and easier to manage.

  • Back Up Regularly: Keep backups of your flows. You can manually export them via the editor or automate backups using scripts.

  • Monitor Performance: Use the built-in debug nodes to monitor the performance and identify bottlenecks.

Example: Simple HTTP Endpoint

Here's a quick example of how you can set up a simple HTTP endpoint that responds with "Hello, World!":

  • Step 1: Drag an HTTP In node onto the workspace and configure it for a GET request at /hello.

  • Step 2: Drag a Function node and connect it to the HTTP In node. Set the function to:

    msg.payload = "Hello, World!";
    return msg;
    
  • Step 3: Drag an HTTP Response node and connect it to the Function node.

  • Step 4: Deploy your flow and test it by navigating to http://<your-server-ip>:1880/hello.

Conclusion

Node-RED is a versatile and powerful tool for anyone looking to get into IoT or API integration on a Linux server. With a little setup and some best practices, you can create robust and efficient workflows to handle a variety of tasks.

The text above is licensed under CC BY-SA 4.0 CC BY SA