Back to all

Getting Started with Node-RED and Mosquitto MQTT Broker

In the fast-evolving environment of the Internet of Things (IoT), bringing ideas to fruition requires innovation and technical know-how. This is where Node-RED comes in. It enables creators to bridge the gap between concept and execution regardless of their coding expertise.

In this article, I explore the world of Node-RED and its role in simplifying the development of robust IoT projects. Moreover, I demonstrate how Node-RED combines with Mosquitto, a powerful and lightweight MQTT broker, to enhance the efficiency and flexibility of your IoT solutions.

What is Node-RED?

Node-RED is a user-friendly open-source tool designed for creating and managing applications primarily related to IoT and home automation. It offers a unique visual approach where you connect various “nodes” on a grid to build your application’s logic. These nodes represent tasks and functions like data processing, device communication, and web services. This visual approach makes it easy for technical and non-technical users to design complex data flows without extensive coding.

Node-RED provides a wide array of built-in nodes that handle different tasks, and it’s particularly well-known for its support of IoT protocols like MQTT and HTTP. Users can also develop their custom nodes using JavaScript. Once you’ve crafted your application’s flow by connecting nodes, you can deploy it to different environments, whether on a local machine, cloud server, or IoT device.

Installing Node-RED

Now, I will demonstrate the installation of Node-RED by running it locally from a Windows PC. The following instructions will assume you are using a Windows operating system

Node-RED is available for different platforms and operating systems. Here, you can find a comprehensive list of supported environments with setup instructions.

Node-RED requires Node.js to work. Therefore, download this first tool from here.

Once the installation completes, it is possible to verify the procedure’s correctness by typing the following command into a Powershell console:

node --version; npm --version

Expect a similar output:

Proof of correct Node.js and npm installation
Figure 1 – Version of Node.js and npm proving the correctness of the installation.

The command in Figure 1 verifies the proper installation of Node.js and npm, which is the Node Packet Manager used for managing Node.js software packages.

After this step, proceed with installing Node-RED by typing the following command in the same Powershell console:

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

Then, the Node-RED setup will start.

Starting the Node-RED environment

After completing the installation, run Node-RED by typing the command below in the same Powershell console:

node-red

Node-RED starts showing the following output:

Output log from Node-RED
Figure 2 – Output log from Node-RED.

The console will stay open with Node-RED now running. If the console closes, this will also terminate the Node-RED process.

While this is the default “start-up” instruction provided in the official tutorial, running it at start-up or as a service is still possible using third-party tools.

To reach the configuration panel, follow this link: http://localhost:1880. The Node-RED editor will load in your browser window and look like Figure 3.

Node-RED configuration panel window
Figure 3 – Configuration panel of Node-RED.

This configuration panel consists of three parts:

  • A grid in the middle where I can design my project.
  • A menu panel on the left with a list of objects, called nodes, which I can drag and drop on the grid to design my configuration.
  • And an information panel on the right, which holds the debug panel, among others.

Node-RED offers many kinds of nodes for implementing different functionalities or system resources.

Nodes exchange messages comprising a topic and a payload, carrying the message information.

Every node can use messages to get information, transform it, and finally send it as output for other nodes. They are graphically interconnected using a line (Figure 4).

Simple inject node and debug node connection using a line
Figure 4 – Simple design with an inject node and a debug node.

Preparing the MQTT broker infrastructure

After this first introduction to Node-RED, I can proceed with configuring the MQTT infrastructure. In this article, I used the free 14-day cloud trial of Pro Edition for Eclipse Mosquitto. However, a 30-day on-premises trial version with basic MQTT HA is also available. It grants access to advanced features such as enhanced security, management APIs, or a high-performance local persistence database.

How to set up a Pro Mosquitto cloud trial

The first step consists of signing up for a free Pro Mosquitto trial. Click here, then select “Cloud” on the menu tab (Figure 5).

Free Pro Mosquitto cloud trial sign-up form
Figure 5 – Signing up for a free Pro Mosquitto Cloud trial.

To set up a Pro Mosquitto broker, I must enter my email address and the server location where I would host my broker. There are several options to choose from.

Complete the form by clicking the “Submit” button and wait for a confirmation mail. The Pro Mosquitto setup procedure will begin upon confirming the mail address – requiring 10 to 15 minutes.

I will receive a second email once the configuration is complete (Figure 6). It will contain important information like the MQTT broker address or the credentials required to access the private configuration area of Mosquitto, called Mosquitto Management Center (MMC).

Pro Mosquitto trial confirmation email with MMC credentials and endpoint URLs
Figure 6 – Confirmation email with MMC credentials and broker endpoint URLs. The URLs are newly generated for every new user subscription.

How to set up clients in Mosquitto Management Center (MMC)

Now that my Pro Mosquitto Cloud trial is active, I can open the MMC panel by typing the link in the email into a browser and successfully login (Figure 7).

Pro Mosquitto Management Center (MMC) dashboard
Figure 7 – Pro Mosquitto Management Center (MMC).

This is the central control dashboard where it is possible to monitor relevant information, like the number of connected clients, exchanged messages, etc. Another essential functionality accessible from this dashboard is “Clients creation,” which I will use in my example.

To demonstrate the Node-RED integration with Mosquitto, I will create two clients: one for Node-RED, named “nodered”, and one used locally from my PC, named “localpc” – where a publisher and a subscriber are located. The role of both clients will be set to “client”. The design of my demonstration will look like Figure 8.

Design demonstrating Node-RED integration with Mosquitto
Figure 8 – Simple design demonstrating the interaction between Node-RED and Mosquitto.

For a detailed guide on creating clients, refer to Cedalo’s extended guides like this one.

Once created, the two clients are visible on the Clients page.

Creating Node-RED and Local PC clients in the MMC Clients page
Figure 9 – Creation of Node-RED and Local PC clients.

Connecting Node-RED to the MQTT broker

With everything now set up to connect with the MQTT broker, open the Node-RED dashboard as demonstrated in the chapter “Starting the Node-RED environment.” Drag an “MQTT in” node and a “Debug” node into the canvas, as shown in the following figure, and make sure to connect the two nodes.

First demonstration setup comprises only a "MQTT-in" and a "Debug" node
Figure 9 – The first demonstration setup comprises only a MQTT-in and a Debug node.

The “MQTT in” node receives messages from a broker, much like a subscribing client. The “Debug” node enables printing all the received data from the “MQTT in” node in a debug window, demonstrating correct data reception.

I can double-click the “MQTT in” node in my design to open the configuration window (Figure 10).

“MQTT in” node configuration window
Figure 10 – Configuration window of the “MQTT in” node.

This window allows me to configure the essential information regarding the broker and the topic to which Node-RED has to subscribe.

Let’s first start with the broker configuration. Click on the pencil button near the “Server textbox to open the specific broker configuration window. (Figure 11).

Broker configuration window
Figure 11 – Broker configuration window.

This window allows the set up of mandatory parameters like the broker address, the port, the protocol version, or the Keep Alive timer.

On the “Server textbox, I will place the address of the Pro Mosquitto MQTT broker, as received per email but also accessible from the MMC dashboard.

For a classical non-encrypted connection, you can keep the port default value to 1883 or switch it to 8883 for TLS-encrypted communication.

I can now switch to the “Security” tab on the same window to configure the Node-RED client credentials (Figure 12).

Client configuration in Node-RED
Figure 12 – Client configuration in Node-RED.

Once done, I can save and go back to the window shown in Figure 10 for configuring the topic.

I will set the subscribing topic to /test/nodered and leave the field “Action” to its default value.

The “MQTT-in” node configuration is complete, and I can now proceed with configuring an “MQTT-out” node to finalize the Node-RED integration demonstration.

Drag and drop an “MQTT-out” node and an inject node on the canvas, then connect the two nodes to obtain the design as shown in Figure 13.

Complete design of an “MQTT-in” and “MQTT-out” node connection
Figure 13- Complete design with an “MQTT-in” and an “MQTT-out” node.

The “MQTT out” node publishes messages over a specific topic, while the “Inject” node creates and sends data to other nodes. In this case, the “Inject” node will cyclically send the current timestamp as an output every second. This output will then be published by the “MQTT-out” node, as shown in Figure 14.

Now, I can double-click on the “MQTT-out” node and perform the configuration, which will almost be identical to the “MQTT-in” node configuration. (Therefore, the specific step-by-step description will be omitted here). The only difference is the topic where the node will publish the message, which is set to /test/localpc in this case.

Configuration of the timestamp inject node for MQTT publishing tests.
Figure 14 – Configuration of the timestamp inject node for MQTT publishing tests.

The configuration is now complete, and I can check the correctness of the MQTT configuration by looking at the broker connection status indicated near the “MQTT-in” and “MQTT-out” nodes, as shown in Figure 15.

Successful broker connection status, indicated by green squares, near the MQTT-in and MQTT-out nodes
Figure 15 – Small green squares near “MQTT-in/out” nodes, indicating broker connection status.

Test the Node-RED configuration

Node-RED is now configured to act as a subscriber and publisher. However, my setup is still not running: I must click the “Deploy” button in the upper-right corner of the Node-RED console window to activate my design.

Once the Node-RED configuration runs, I can create a local publisher and subscriber hosted by my PC, which will interact with Node-RED.

I can start by testing the “MQTT-in” node operation, which means I will try to publish a message from a local publisher client to the Node-RED client. I will create a publisher client using the MQTT.js client library through the following command into a Powershell console:

npx mqtt pub -h hg13dtohpgob-tvqx2k3tdgzf.cedalo.dev -t /test/nodered -m "Message to Node-RED" -u localpc -P testpwd

If the library is not installed on your machine, the packet manager will automatically prompt a request for installation. Another possibility would be to use the client utilities provided by the open-source version of Mosquitto, for which professional technical support is available.

This command will publish a message on the topic /test/nodered, logging as the localpc client. The message will reach the Node-RED “MQTT-in” node and display it on the debug console, courtesy of the debug node (Figure 16).

A published message is displayed in the Node-RED debug window
Figure 16 – Node-RED debug window.

Now, let’s test the second part of my configuration to verify the “MQTT-out” node effectively publishes the injected data.

I will create a subscriber client on the PC using the MQTT.js client library with the following command:

npx mqtt sub -h hg13dtohpgob-tvqx2k3tdgzf.cedalo.dev -t /test/localpc -u localpc -P testpwd

The subscriber will subscribe to the specified topic and receive the data published from Node-RED, then depict a list of published timestamps (Figure 17).

List of timestamps published by Node-RED and received by the subscriber client
Figure 17 – Timestamps value published by Node-RED and received by the subscriber client.

Conclusion

In summary, integrating the Pro Mosquitto MQTT broker with Node-RED offers a powerful combination for IoT and automation applications. The steps in this article demonstrate the seamless Mosquitto and Node-RED connection. The real-case test highlights the integration’s robustness, and also demonstrates the process of creating clients to enhance security.

This synergy provides a versatile platform for creating responsive IoT applications for home automation or industrial monitoring. The Pro Mosquitto broker’s reliability provides a solid foundation for your projects.

Click to rate this post!
[Total: 1 Average: 5]
About the author
Matteo Trovo Cedalo author

Matteo Trovò

Senior Embedded Software Engineer

Matteo Trovò is a seasoned embedded software engineer with extensive expertise in cybersecurity, computer networks, and data protocols, including MQTT. He is currently employed as a Senior Embedded Software Engineer in the automotive industry in Germany.

After completing a degree in Software Engineering, Matteo gained valuable experience working for various companies, honing his skills in practical settings. During this time, he also supported and contributed to a startup developing MQTT interconnected smart switches for home automation. As the software development lead, Matteo delved deep into MQTT, writing MQTT client software and managing MQTT device networks.

Newsletters icon

Subscribe for monthly updates