Node-RED: MQTT server

How to connect Node-RED to 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 comes with a built-in MQTT server, making it easy to integrate your IoT devices through secure and reliable communication channels.

To get started, simply go to the Stackhero dashboard, find 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 using port 8883. All connections are secured with TLS encryption, ensuring your data stays protected.

If you are new to MQTT with Node-RED, here is a simple example to help you get up and running. This example shows 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-REDSimple example of connecting MQTT to Node-RED

If your MQTT nodes are stuck on a "connecting" status, it is a good idea to check whether the MQTT authentication flow is still in place. If it is missing, you can download it here: mqttAuthenticationFlow.json. Import this flow into your Node-RED instance, update the Users node with your preferred passwords, and remove any unnecessary comments as needed.

Managing MQTT user authentication is straightforward with a dedicated flow in Node-RED. You can find the "MQTT Authentication" flow directly from the Node-RED admin panel.

MQTT authentication flowMQTT authentication flow

Inside this authentication flow, look for the Users template node. This node holds an array where you can define credentials for each user. You have the flexibility to add as many users as your project needs.

Note: The node-red user is essential for Node-RED's connection to the MQTT server. Make sure not to remove this user from your configuration.

MQTT users nodeMQTT users node

You have the flexibility to connect to the Mosquitto MQTT server using either the MQTT protocol or WebSockets.

While the MQTT protocol is lightweight, making it a great fit for IoT devices, it is not natively supported in web browsers. If you want to interact with your MQTT server from a browser, you will want to use WebSockets. This approach lets you encapsulate MQTT traffic within the WebSocket protocol, and it works seamlessly with Stackhero's services.

To enable WebSocket connectivity, simply activate the "WebSocket Support" option in your Stackhero dashboard. Once enabled, you can configure your client to use the WebSocket URL: wss://<XXXXXX>.stackhero-network.com:443/mqtt. Make sure to substitute the default mqtts://<XXXXXX>.stackhero-network.com with this WebSocket URL for browser-based connections.

For development, you might find the "MQTT.js" library especially helpful. It is a robust JavaScript library compatible with both Node.js and browser environments. You can learn more by checking out its official repository.

If you are looking for a practical example of how to connect from your browser to the MQTT server using 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 fit your setup.

MQTT WebSocket exampleMQTT WebSocket example

Before you try this setup, make sure that WebSocket support is enabled in your Stackhero dashboard. This ensures everything runs smoothly for browser-based connections.