Skip to main content

mqtt-out

Publish messages to MQTT topics on configured brokers.

mqtt-out
output v1.0.0

Publish messages to MQTT topics. Supports dynamic topics, QoS levels, retained messages, and MQTT 5.0 features.

mqtt publish iot messaging
Inputs
1
Outputs
0

Overview

The mqtt-out node publishes messages to an MQTT broker. Use it to:

  • Send sensor data - Publish readings to cloud platforms
  • Control devices - Send commands to other IoT devices
  • Bridge systems - Forward data between different services
  • State updates - Publish device status with retained messages

The node shares broker configuration with mqtt-in nodes, so you only need to configure connection details once.

Properties

Property Type Required Default Description
name string - - Display name for this node in the editor.
broker config - MQTT broker configuration node containing connection details.
topic string - - MQTT topic to publish to. Can be overridden by msg.topic.
qos number - 0 Quality of Service level: 0 (at most once), 1 (at least once), 2 (exactly once).
retain boolean - false If true, the message will be retained by the broker.
respTopic string - - MQTT 5.0 response topic for request/response patterns.
contentType string - - MQTT 5.0 content type (e.g., "application/json").
userProps object - - MQTT 5.0 user properties as key-value pairs.
expiry number - - MQTT 5.0 message expiry interval in seconds.

Inputs

The mqtt-out node accepts messages with the following properties:

Property Type Description
msg.payload any The message content to publish (auto-converted to string/buffer)
msg.topic string Overrides the configured topic
msg.qos number Overrides the configured QoS (0, 1, or 2)
msg.retain boolean Overrides the configured retain flag
Payload Conversion

Objects are automatically JSON-stringified. Numbers and booleans are converted to strings. Buffers are sent as-is (binary). To send raw JSON, use JSON.stringify() first.

Dynamic Topics

You can set the topic dynamically using msg.topic. This is useful for:

  • Publishing to different topics based on sensor ID
  • Routing messages to device-specific channels
  • Creating hierarchical topic structures

Topic Naming Conventions

# Good topic structure
sensors/living-room/temperature
devices/raspberry-pi-01/status
home/floor1/room2/light/state
commands/device-id/action

# Avoid
my topic          # No spaces
sensors//temp     # No double slashes
/sensors/temp     # Don't start with /

Retained Messages

When a message is published with the retain flag set to true:

  • The broker stores the last message for that topic
  • New subscribers immediately receive the retained message
  • Useful for status updates, configurations, and last-known values
Clear Retained Message

To clear a retained message, publish an empty message (empty string or null) with retain=true to the same topic.

Examples

Publish Sensor Data

Read temperature from a sensor and publish to MQTT.

Temperature Publisher

Reads DHT22 sensor and publishes temperature to MQTT.

View Flow JSON
[
  {
    "id": "sensor1",
    "type": "dht22",
    "name": "DHT22",
    "pin": 4,
    "wires": [["function1"]]
  },
  {
    "id": "function1",
    "type": "function",
    "name": "Format",
    "func": "msg.payload = msg.payload.temperature;\nmsg.topic = 'sensors/living-room/temperature';\nreturn msg;",
    "wires": [["mqtt1"]]
  },
  {
    "id": "mqtt1",
    "type": "mqtt-out",
    "name": "Publish",
    "broker": "broker1",
    "qos": "1",
    "retain": false
  }
]

JSON Payload

Publish structured JSON data with multiple fields.

JSON Sensor Data

Publishes a JSON object with temperature, humidity, and timestamp.

View Flow JSON
[
  {
    "id": "sensor2",
    "type": "dht22",
    "name": "DHT22",
    "pin": 4,
    "wires": [["function2"]]
  },
  {
    "id": "function2",
    "type": "function",
    "name": "Build JSON",
    "func": "msg.payload = {\n  temperature: msg.payload.temperature,\n  humidity: msg.payload.humidity,\n  timestamp: new Date().toISOString(),\n  device: 'rpi-01'\n};\nmsg.topic = 'sensors/dht22/data';\nreturn msg;",
    "wires": [["mqtt2"]]
  },
  {
    "id": "mqtt2",
    "type": "mqtt-out",
    "name": "Publish JSON",
    "broker": "broker1",
    "qos": "1"
  }
]

Dynamic Topics per Device

Route messages to different topics based on device ID.

Multi-Device Publisher

Receives data from multiple sensors and publishes to device-specific topics.

View Flow JSON
[
  {
    "id": "input1",
    "type": "mqtt-in",
    "name": "All Sensors",
    "topic": "raw/sensors/#",
    "wires": [["function3"]]
  },
  {
    "id": "function3",
    "type": "function",
    "name": "Route by Device",
    "func": "// Extract device ID from topic\nconst parts = msg.topic.split('/');\nconst deviceId = parts[2];\n\n// Build new topic\nmsg.topic = 'processed/' + deviceId + '/data';\n\n// Add metadata\nmsg.payload = {\n  value: msg.payload,\n  device: deviceId,\n  processed: Date.now()\n};\n\nreturn msg;",
    "wires": [["mqtt3"]]
  },
  {
    "id": "mqtt3",
    "type": "mqtt-out",
    "name": "Publish Processed",
    "broker": "broker1",
    "qos": "1"
  }
]

Troubleshooting

Messages not being published
  • Check the broker connection status (node shows connected/disconnected)
  • Verify the topic is set (either in config or msg.topic)
  • Use an MQTT client to subscribe and verify messages arrive
  • Check EdgeFlow logs for connection errors
Topic shows as undefined

Either set the topic in the node configuration or ensure your flow sets msg.topic before the mqtt-out node. You can use a change node to set msg.topic:

Set msg.topic to "sensors/temperature"
Payload shows as [object Object]

Objects are automatically JSON-stringified when published. If you're seeing [object Object], make sure you're not accidentally converting to string elsewhere. Debug before mqtt-out to verify the payload structure.

Retained messages not working
  • Verify retain is enabled in the node or set msg.retain = true
  • Some brokers disable retained messages - check broker configuration
  • Use a new subscriber to test (existing ones may have received it already)
High QoS messages slow down flow

QoS 1 and 2 require acknowledgments which add latency. For high-frequency sensor data where occasional loss is acceptable, use QoS 0. Reserve QoS 1/2 for critical commands and alerts.