Jedsada Srijunpoe

« Home

Create a Node-RED flow for publishing and subscribing messages

In this blog, we will create a Node-RED flow for publishing and subscribing messages to a public MQTT broker.

complete flow


1. Add an inject node

inject node

Edit its properties:

inject node properties

Each inject node generates a message that you can edit some properties such as node name, message payload, and additional user-defined properties.

In this case, we use timestamp as a message payload and inject once after 0.1 seconds, then repeat for every 5 seconds.


2. Add a function node

function node

Edit its properties:

inject node properties

// Convert the payload to a String object
msg.payload = new Date(msg.payload).toString();
return msg;

This JavaScript code is used to convert the incoming payload of the message (msg.payload) which is a timestamp value (in seconds) into a JavaScript Datestring.


3. Add a mqtt in node and a mqtt out node

mqtt in node mqtt out node

Edit its properties:
First, we need to add new MQTT-broker config node by clicking at this pen icon when edit mqtt in node properties.

add new mqtt-broker1

We will test it by using Mosquitto public MQTT broker “test.mosquitto.org” with port 1883.

add new mqtt-broker2

Edit mqtt in node properties:

mqtt in node properties

Edit mqtt out node properties:

mqtt out node properties


4. Add a debug node

debug node

complete flow

After we wire to connect all node together, we can click to deploy and see the debug output.

debug output