Node-RED MQTT Node — Complete Guide
In this tutorial, you'll learn about Node. We cover key concepts, practical examples, and best practices.
Hook
Your Node-RED MQTT node shows a red dot (disconnected) or receives no messages even though the dot is green. The MQTT broker is running, other clients can publish and subscribe, but Node-RED's MQTT input node stays silent.
The Wrong Way
Creating multiple MQTT broker configurations for the same broker causes Node-RED to open duplicate connections, which can exceed the broker's client limit.
# BAD: Duplicate broker configs
# flow1: MQTT broker "My Broker" (192.168.1.50:1883)
# flow2: MQTT broker "My Broker 2" (192.168.1.50:1883)
Broker: 2 connections from same client ID
Connection rejected: "Client ID already connected"
Duplicate connections cause broker to disconnect both
Each MQTT broker config creates a separate connection. Reuse one config for all MQTT nodes.
The Right Way
Use a single MQTT broker configuration and verify connection details.
# 1. Check the MQTT broker node status
# Look at the small dot under the MQTT broker config node
# Red: disconnected, Green: connected
# 2. Test the connection manually
mosquitto_pub -h 192.168.1.50 -p 1883 -u user -P pass -t test -m "hello"
mosquitto_sub -h 192.168.1.50 -p 1883 -u user -P pass -t test -C 1
(messages flow correctly)
If the manual test works but Node-RED does not:
# 3. Check the broker config node
# Double-click → Edit → Verify:
# - Server: 192.168.1.50
# - Port: 1883
# - Client ID: (unique, leave blank for auto)
# - Keep alive: 60
# - Clean session: true
# 4. Enable MQTT debugging in settings.js
# In ~/.node-red/settings.js:
mqtt:
debug: true
# 5. Restart Node-RED and watch the logs
node-red-stop && node-red-start
journalctl -u node-red -n 50 -f
[info] [mqtt-broker:My Broker] Connected to broker at 192.168.1.50:1883
[info] [mqtt-broker:My Broker] Subscribed to topic: home/#
Prevention
- Reuse a single broker config node for all MQTT in/out nodes.
- Use a unique client ID for Node-RED or leave it blank for auto-generation.
- Set "Clean session: true" for development, "false" for production to receive queued messages.
- Test with
mosquitto_pub/subbefore blaming Node-RED. - Monitor the broker's connection list to verify Node-RED is connected.
Common Mistakes with red mqtt node
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world NODE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
DodaTech — MQTT messages that arrive in Node-RED.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro