Mosquitto: MQTT API authentication with Node-RED
This documentation is part of the API authentication guide. You can view the complete guide here: How to authenticate thousands of devices using an external API.
👋 Welcome to the Stackhero documentation!
Stackhero offers a ready-to-use Mosquitto MQTT cloud solution that provides a host of benefits, including:
- Unlimited message exchanges and transfers.
- Unlimited devices authentication via an external API.
- Advanced ACLs on topics, users and actions.
- Customisable domain name secured with HTTPS (for example, https://mqtt.your-company.com).
- Effortless updates with just a click.
- 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 Mosquitto MQTT cloud hosting solution!
Stackhero for Node-RED includes a Mosquitto server. Nevertheless, for advanced usage, having a standalone Mosquitto service is recommended. This example shows how to use the API authentication system with a standalone Mosquitto service and a Node-RED service. If you use the Mosquitto service included with your Node-RED, this guide does not apply to you.
Stackhero for Node-RED provides an example of an MQTT API connection in the flow named "MQTT authentication." This flow is also used with the integrated Mosquitto server in Stackhero for Node-RED.
Configuring Mosquitto to use Node-RED as authentication API
To use Node-RED as your API authentication endpoint, update your Mosquitto configuration as follows:
- Enable the
API authenticationoption. - Set the
Hostfield to your Node-RED domain. - Choose
HTTPSas theProtocoland enter443for thePort. - Specify the
User routeas/mqttAuthentication/userGet. - Specify the
ACLs routeas/mqttAuthentication/aclCheck.
For debugging, you might set the "Authentication cache time" to 1 second. Once your setup is operating smoothly, the recommended value is 30 seconds.
Mosquitto configuration example
Configuring Node-RED as an MQTT authentication API
After updating the Mosquitto configuration, launch Node-RED and open the MQTT authentication flow. Edit the Users node to configure your user credentials. The provided examples include several username and password combinations. Feel free to add as many users as necessary.
Node-RED MQTT authentication flow