Build a working Node-RED and MQTT connection
MQTT lets IoT devices exchange messages through a broker instead of connecting directly to each other. In this guide, connect Node-RED to a Mosquitto broker, publish a test message, subscribe to the same topic, and confirm the message appears in the Debug sidebar.
You need a running Node-RED instance, access to a Mosquitto broker, and its hostname or IP address, port, and credentials if authentication is enabled. No sensor or ESP32 is required for this first test.
1. Understand MQTT publish and subscribe
MQTT uses a publish/subscribe model. A publisher sends a message to a named topic; the broker receives it and forwards it to clients subscribed to that topic. A subscriber does not need to know the publisher's network address.
Mosquitto is an open-source MQTT broker. Node-RED's MQTT Out node publishes messages, while MQTT In subscribes and passes received messages into a flow.
Node-RED MQTT Out → Mosquitto broker → Node-RED MQTT In → Debug
topic: solderhub/test
For this tutorial, use topic solderhub/test and message Hello from SolderHub.
2. Find your Mosquitto broker address
If Mosquitto and Node-RED run on the same machine, the broker may be reachable at 127.0.0.1 on port 1883, provided Mosquitto is listening there. If Mosquitto runs on another computer, use that computer's LAN IP address or hostname instead.
Example settings (replace them with your actual configuration):
| Setting | Example |
|---|---|
| Server | 192.168.1.50 |
| Port | 1883 |
| Topic | solderhub/test |
| QoS | 0 |
Important: 127.0.0.1 always means the machine where the connecting application runs. If Node-RED and Mosquitto are on different computers, 127.0.0.1 in Node-RED will not reach the other computer.
On a Linux host where Mosquitto is managed by systemd, check its state with:
sudo systemctl is-active mosquitto
A running service normally reports active. If the service is missing or inactive, check the broker installation and configuration first.
3. Configure the broker connection in Node-RED
Drag an MQTT Out node onto the workspace and open its configuration. Create or select a broker configuration, then enter the Mosquitto hostname or IP address and configured port. Port 1883 is commonly used for unencrypted MQTT; port 8883 is commonly used for MQTT over TLS, but use the settings configured by your broker.
Enter a username and password if authentication is enabled. Configure TLS and certificate settings if required. Save the broker configuration, then select the same broker when configuring MQTT In.
If the broker is remote, ensure it listens on an interface Node-RED can reach and that network rules permit the connection. Do not expose an unauthenticated broker directly to the public internet. For remote access, prefer a VPN or properly configured TLS, authentication and topic access controls. Never include real credentials in flow labels, screenshots or article examples.
4. Publish your first MQTT message
Build a publisher flow using Inject → MQTT Out.
- Add an Inject node and set its payload type to string, with value
Hello from SolderHub. - Set the topic to
solderhub/test. Depending on your Node-RED version, setmsg.topicin Inject or with a Change node, or configure the topic directly in MQTT Out. - Configure MQTT Out to use the Mosquitto broker you saved. If the topic is set in MQTT Out, let Inject supply only the payload.
- Connect Inject to MQTT Out and click Deploy.
- Click Inject's small button to publish the message.
[Inject: Hello from SolderHub] → [MQTT Out] → Mosquitto
topic: solderhub/test
A publish action alone does not prove a subscriber received the message; we will verify delivery next. MQTT topics are case-sensitive, so solderhub/test and SolderHub/test are different topics. For real projects, use a consistent structure such as solderhub/room1/temperature.
5. Subscribe and verify the message
- Drag an MQTT In node onto the workspace and open its configuration.
- Select the same broker used by MQTT Out.
- Set the topic to
solderhub/test; QoS0is sufficient for this test. - Add a Debug node and connect MQTT In to Debug.
- Configure Debug to display
msg.payloadand make sure it is enabled. - Click Deploy, then click the Inject button again.
The Debug sidebar should show:
Hello from SolderHub
The complete workflow is working: Inject created the message, MQTT Out published it, Mosquitto routed it by topic, MQTT In received it, and Debug displayed the payload.
PUBLISH: [Inject] → [MQTT Out] → Mosquitto
│ topic: solderhub/test
SUBSCRIBE: [MQTT In] ←───────────────┘
│
▼
[Debug]
Publish a fresh message after the subscriber is deployed to make the result easy to interpret.
6. QoS and retained messages
Quality of Service (QoS) controls the MQTT delivery guarantee:
- QoS 0: At most once; no MQTT delivery acknowledgement.
- QoS 1: At least once; duplicates can occur.
- QoS 2: Exactly once at the MQTT protocol level, using a more involved exchange.
QoS 0 is usually enough for the first test. For production systems, choose a level based on the impact of lost or duplicated messages and test how your application handles them.
A retained message lets the broker store the latest retained value for a topic and send it to new subscribers. It is useful for the latest device status, but it is not a history of readings. Store measurements in a database if you need historical data.
7. Troubleshoot common problems
MQTT node stays on “connecting”
Check that Mosquitto is running, the host and port are correct, and the broker listens on an interface Node-RED can reach. For remote brokers, check routing and firewall rules.
Connection refused
The broker may be stopped, the port may be wrong, the listener may accept only local connections, or the client may use the wrong security settings. Review Mosquitto's configuration and logs.
Authentication fails
Check the username, password, and any required client certificate. Do not disable authentication as a shortcut on a network-accessible broker.
MQTT Out seems to work, but Debug is empty
Make sure MQTT In uses the same broker and exact topic. Confirm MQTT In is deployed and connected, Debug is enabled, and the Debug sidebar is open. Publish a new message after the subscriber connects.
Works on the Pi but not from another computer
Mosquitto may listen only on loopback, or a firewall may block incoming MQTT connections. Review listener and access-control settings rather than exposing the broker without protection.
8. Connect a real IoT device next
Once the test works, replace Inject with a real publisher. An ESP32 can read a temperature sensor and publish a numeric reading to solderhub/room1/temperature. Node-RED can subscribe to that topic, validate the payload, and send it to a dashboard, database, or alert flow.
Keep responsibilities clear: ESP32 firmware reads the sensor and publishes data; Mosquitto routes MQTT messages; Node-RED processes and connects data to other services. Add payload validation, stale-data checks, authentication and suitable error handling before relying on readings for automation.
Frequently asked questions
Does Node-RED include an MQTT broker?
No. MQTT In and MQTT Out are client nodes. Mosquitto or another broker routes messages between clients.
Can Node-RED publish and subscribe to the same topic?
Yes. Configure MQTT Out to publish to a topic and MQTT In to subscribe to that same topic through a reachable broker.
Can an ESP32 send sensor readings to Node-RED?
Yes. The ESP32 can publish readings through MQTT, and Node-RED can subscribe and process them. The ESP32 still needs firmware to read its sensor and publish data.
Do I need a public broker?
No. A local Mosquitto broker is enough for projects on your local network. Remote access needs deliberate network and security configuration.
Is MQTT the same as Node-RED?
No. MQTT is a messaging protocol; Node-RED is a flow-based tool that can publish, subscribe to, transform, and route MQTT messages.
Conclusion
You have connected Node-RED to Mosquitto, published Hello from SolderHub to solderhub/test, subscribed to that topic, and verified the payload in Debug. This small test is the foundation for a larger IoT workflow.
Next, connect an ESP32 to Mosquitto and build a temperature monitor with validated sensor data and a dashboard.
Before publication: add the final original diagrams and screenshots to the six image placeholders. Keep credentials, private IP addresses and tokens out of screenshots.
Related SolderHub guides
- Node-RED Tutorial for Beginners: Nodes, Flows & msg.payload
- Install Node-RED on Raspberry Pi 5: Autostart & Remote Editor
Publish the related guides before relying on these internal links.