Code samples

Node-RED MQTT example

Connect Node-RED to an MQTT broker with an importable flow. Start on the free public broker, then point the same code at a secure private broker on ConnectMQ.

Source code on GitHubmqtt-code-samples/node-redClone the complete, runnable examples. Stars and pull requests welcome. View on GitHub

1. Try it on the free public broker

No account needed: connect to broker.connectmq.com on port 1883 and start publishing in seconds.

flow.json
[
  { "id": "cmq_broker", "type": "mqtt-broker", "name": "ConnectMQ broker",
    "broker": "broker.connectmq.com", "port": "1883", "usetls": false,
    "protocolVersion": "4", "keepalive": "60", "cleansession": true },
  { "id": "cmq_inject", "type": "inject", "name": "Send a reading every 10 s", "x": 160, "y": 80,
    "props": [{ "p": "payload" }], "payload": "{\"temperature\":21.5}", "payloadType": "json",
    "repeat": "10", "once": true, "wires": [["cmq_out"]] },
  { "id": "cmq_out", "type": "mqtt out", "name": "Publish a JSON reading", "x": 420, "y": 80,
    "topic": "your-project/sensors/temperature", "qos": "1", "retain": "false",
    "broker": "cmq_broker", "wires": [] },
  { "id": "cmq_in", "type": "mqtt in", "name": "Subscribe to the temperature topic", "x": 160, "y": 160,
    "topic": "your-project/sensors/#", "qos": "1", "datatype": "auto-detect",
    "broker": "cmq_broker", "wires": [["cmq_debug"]] },
  { "id": "cmq_debug", "type": "debug", "name": "Runs for every message on a subscribed topic", "x": 420, "y": 160,
    "active": true, "complete": "payload", "wires": [] }
]

Free public MQTT broker

The public broker is shared: anyone can subscribe to your topics. Never send passwords, personal data or anything confidential through it.

About the free public MQTT broker

2. Go to production with ConnectMQ

For real devices, use a private serverless MQTT broker on ConnectMQ instead of the public one: TLS encryption, a username and password for every device and per-topic access control, fully managed. Start on the Free plan — no card required.

Replace YOUR_BROKER_HOST, YOUR_USERNAME and YOUR_PASSWORD with the values from your ConnectMQ dashboard.

flow.json
[
  { "id": "cmq_tls", "type": "tls-config", "name": "TLS: the system CA store verifies the broker certificate", "verifyservercert": true },
  { "id": "cmq_broker", "type": "mqtt-broker", "name": "ConnectMQ broker",
    "broker": "YOUR_BROKER_HOST", "port": "8883", "usetls": true, "tls": "cmq_tls",
    "protocolVersion": "4", "keepalive": "60", "cleansession": true,
    "credentials": { "user": "YOUR_USERNAME", "password": "YOUR_PASSWORD" } },
  { "id": "cmq_inject", "type": "inject", "name": "Send a reading every 10 s", "x": 160, "y": 80,
    "props": [{ "p": "payload" }], "payload": "{\"temperature\":21.5}", "payloadType": "json",
    "repeat": "10", "once": true, "wires": [["cmq_out"]] },
  { "id": "cmq_out", "type": "mqtt out", "name": "Publish a JSON reading", "x": 420, "y": 80,
    "topic": "your-project/sensors/temperature", "qos": "1", "retain": "false",
    "broker": "cmq_broker", "wires": [] },
  { "id": "cmq_in", "type": "mqtt in", "name": "Subscribe to the temperature topic", "x": 160, "y": 160,
    "topic": "your-project/sensors/#", "qos": "1", "datatype": "auto-detect",
    "broker": "cmq_broker", "wires": [["cmq_debug"]] },
  { "id": "cmq_debug", "type": "debug", "name": "Runs for every message on a subscribed topic", "x": 420, "y": 160,
    "active": true, "complete": "payload", "wires": [] }
]

Node-RED MQTT FAQ

How do I connect Node-RED to an MQTT broker?

Install mqtt in / mqtt out nodes, connect to the broker host on port 1883 (or 8883 with TLS), then subscribe to a topic and publish messages to it. The examples on this page connect to the free public broker broker.connectmq.com and to a private ConnectMQ broker.

Is there a free MQTT broker for testing Node-RED?

Yes. broker.connectmq.com is a free public MQTT broker that needs no account. Anyone can read its topics, so use it only for tests and use a unique topic prefix.

How do I secure a Node-RED MQTT connection?

Connect over TLS on port 8883 with a username and password from your ConnectMQ dashboard, and give every device its own credentials and topic permissions instead of using the public broker.

Free public MQTT broker

Test any example on broker.connectmq.com — no sign-up, MQTT 3.1.1 and 5, TCP, TLS and WebSocket.

About the free public MQTT broker

Secure serverless MQTT for production

When it is time for real devices, run the same code on a private, fully managed ConnectMQ broker with TLS and per-device credentials.

Start for free

Your private MQTT broker is a few clicks away

Create an account, get your broker credentials and connect your first device — no servers to install, patch or scale.