In this blog, we will create a Node-RED flow for publishing and subscribing messages to a public MQTT broker.
Edit its 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.
Edit its 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.
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.
We will test it by using Mosquitto public MQTT broker “test.mosquitto.org” with port 1883.
Edit mqtt in node properties:
Edit mqtt out node properties:
After we wire to connect all node together, we can click to deploy and see the debug output.