Node-RED: MQTT Server
How to connect Node-RED to the Mosquitto MQTT server
👋 Welcome to the Stackhero documentation!
Stackhero offers a ready-to-use Node-RED cloud solution that provides a host of benefits, including:
- MQTT server included (Mosquitto).
- Full access to Node-RED admin UI.
- Node-RED Dashboard included.
- Unlimited and dedicated SMTP email server included.
- Effortless updates with just a click.
- Customizable domain name secured with HTTPS (for example, https://node-red.your-company.com).
- Optimal performance and robust security powered by a private and dedicated VM.
Save time and simplify your life: it only takes 5 minutes to try Stackhero's Node-RED cloud hosting solution!
Stackhero's Node-RED service includes an integrated MQTT server, making it easy to connect your IoT devices through secure and reliable communication channels.
To get started, simply go to your Stackhero dashboard, select your Node-RED service, and enable the "MQTT Server" option in the service configuration settings.
Once the server is enabled, you can connect to it on port 8883. All connections are protected with TLS encryption, ensuring your data remains secure.
Simple example: Using Node-RED with the MQTT server
If you're just getting started with MQTT in Node-RED, here's a straightforward example to help you get up and running quickly. This example demonstrates how to send a message to a topic and receive messages from it. You can download the flow example and import it into your Node-RED instance using the "Import" menu.
Simple example of connecting MQTT to Node-RED
If your MQTT nodes are stuck in the "connecting" state, it's a good idea to check if the
MQTT authenticationflow is still present. If it's missing, you can download it here: mqttAuthenticationFlow.json. Import this flow into your Node-RED instance, update theUsersnode with your chosen passwords, and remove any unnecessary comments as needed.
Authenticating MQTT users in Node-RED
Managing MQTT user authentication is simple with a dedicated flow in Node-RED. You can find the "MQTT Authentication" flow directly from the Node-RED admin panel.
MQTT authentication flow
Within this authentication flow, look for the Users template node. This node contains an array where you can define credentials for each user. You can add as many users as your project requires.
Note: The
node-reduser is required for Node-RED to connect to the MQTT server. Be sure not to remove this user from your configuration.
MQTT users node
Connecting to MQTT via WebSockets
You have the option to connect to the Mosquitto MQTT server either using the MQTT protocol or via WebSockets.
While the MQTT protocol is lightweight and especially suitable for IoT devices, it is not natively supported by web browsers. If you want to interact with your MQTT server from a browser, it's recommended to use WebSockets. This method encapsulates MQTT traffic within the WebSocket protocol and works seamlessly with Stackhero's services.
To enable WebSocket connectivity, simply activate the "WebSocket Support" option in your Stackhero dashboard. Once enabled, configure your client to use the WebSocket URL: wss://<XXXXXX>.stackhero-network.com:443/mqtt. Be sure to replace the default mqtts://<XXXXXX>.stackhero-network.com with this WebSocket URL for browser-based connections.
Choosing an MQTT library
For development, the "MQTT.js" library is highly recommended. It's a robust JavaScript library compatible with both Node.js and browser environments. You can find more information on its official repository.
Example: Connecting from browser to MQTT via WebSockets
For a concrete example of connecting from a browser to the MQTT server via WebSockets, you can refer to this repository: https://github.com/stackhero-io/mqttWebSocketGettingStarted. Feel free to modify the src/index.html file with your server's url, username, and password to match your setup.
MQTT WebSocket example
Before testing this setup, make sure that WebSocket support is enabled in your Stackhero dashboard. This will ensure browser-based connections work properly.