Publish and subscribe to real-time messages with Node-RED
In this page, you will see how to publish and subscribe to real-time Data Streams with Node-Red.
For this section, you'll first need to connect your device to the cloud, create a remote connection for Node-RED, and create a Device API Key.
In case you have a Node-RED already installed manually and only need to exchange real-time data without managing your device, you can add IoTFlows Node-RED Nodes manually to your Node-RED. Learn how to add nodes to Node-RED.
From the side navigation tab of the device page, click on Create Data Stream. Set the name, description, and units to the type of data that you will be receiving.
Ensure your device is connected to the cloud by checking if you see the green light on the device page, Enable the Node-RED connection in the Remote Connections tab, and hit Connect.
IMPORTANT: We strongly recommend that you change the default username and password for NodeRED (admin, password). Learn how to change the Node-RED credentials.
From the nodes in the left navigation bar, drag and drop the datastream out node.
Double-click on the datastream out node and click on the edit button for the server
Here we will add the Device API Key information that will be used to publish data into our table.
If you have previously configured any nodes with your Device API KEY username and password, your nodes should already be configured.
Next, we will click the refresh button in the Topic row and select our Data Stream and hit Done.
Any message payloads sent to the data streams out node will be published to the cloud through our data stream. You can connect any sensor that is receiving data into the data streams out node and click Deploy.
For the purpose of simplicity, we will use an inject node and trigger it to publish a datapoint when it is clicked. Drag and drop an inject node to and connect it the data streams out.
Double-click on the inject node and configure it to publish a value of 10 with a 5 seconds intervals and click Done.
Click Deploy to deploy your code. Now, the inject node generates a message with msg.payload = 10 every 5 seconds and the data stream out node publishes the data to the cloud through our data stream. You can see real-time data coming shown in the console.
You can subscribe to your data streams and receive the messages published by other apps/clients in real-time. For the purpose of demonstration only, we can subscribe to the Temperature data stream and verify that we can get the same data.
Drag and drop a datastream in node, double click on it, and select the Temperature data stream in the Topic list.
Connect the output of the datastream in node to a debug node and hit Deploy.
You should see that we receive a message with the payload of "10" from the cloud every 5 seconds.
Congratulations! You have successfully published and subscribed to secured real-time data streams over the internet.