mqtt-relay is a flexible MQTT relay module that forwards messages from one broker to another. It allows you to selectively map and transform topics, enabling you to set custom output topics while preserving subtopic structures. This functionality is particularly useful in complex setups where specific MQTT topics need to be routed or modified between brokers.
To install mqtt-relay globally, use npm:
npm install -g mqtt-relayNote: You might see warnings about deprecated dependencies during installation. These are typically related to development dependencies and do not affect the functionality of mqtt-relay.
After installation, you can initialize a sample configuration file in your current directory:
mqtt-relay initThis will create a relay-config.yaml file. Edit this file to specify your MQTT brokers, relay options, and topic mappings.
Then, run mqtt-relay with your configuration file:
mqtt-relay relay-config.yamlYou can run multiple instances with different configurations. For each instance, specify the config file as a parameter:
mqtt-relay another-config.yamlWith the provided program, you can also configure several setups in a single configuration as shown in the sample configuration file, giving each instance a name. The name is shown in the logs.
To manage instances of mqtt-relay that need to run unattended, you can use PM2. Set each instance with a unique name:
pm2 --name "mqtt-relay-instance" mqtt-relay -- relay-config.yamlThe configuration file allows you to define both input and output brokers, publishing options, and detailed topic mappings for flexible topic transformations.
name: mqtt-relay-example
brokerInUrl: "mqtt://input-broker.example.com:1883"
brokerInOptions:
username: "inputUser"
password: "inputPassword"
brokerOutUrl: "mqtt://output-broker.example.com:1883"
brokerOutOptions:
username: "outputUser"
password: "outputPassword"
publishOptions:
retain: true
qos: 1
debug: false
topicMap:
- in: "device123/sensor/#"
out: "home/sensors"
- in: "monitoring/temperature"
out: "metrics/temperature"
- in: "alerts/#"
out: "notifications/alerts"
- in: "system/status"
out: "status/system"topicMap specifies how each input topic (in) is transformed before it is published to the output broker. This allows you to relay topics directly or modify them based on custom mappings.
-
Prefix Match with Dynamic Subtopics:
- If the
intopic ends with/#, it will match the topic prefix and relay any additional subtopic structure. - Example:
- in: "device123/sensor/#" out: "home/sensors"
- Incoming topic:
device123/sensor/temperature/reading - Published topic:
home/sensors/temperature/reading
- Incoming topic:
- If the
-
Exact Match:
- If the
intopic does not end with/#, it will only match the specific topic exactly as written. - Example:
- in: "monitoring/temperature" out: "metrics/temperature"
- Incoming topic:
monitoring/temperature - Published topic:
metrics/temperature
- Incoming topic:
- If the
-
Pass-Through (No
outSpecified):- If no
outfield is provided, the input topic will be passed through unmodified. - Example:
- in: "alerts/#"
- Incoming topic:
alerts/high - Published topic:
alerts/high
- Incoming topic:
- If no
To construct the topicMap:
in: Specify the incoming topic to match. Use+to match one level and/#at the end to match all remaining subtopics.out(optional): Specify the output topic prefix. If/#is used inin, the remaining subtopics will be appended toout.
Examples:
-
Dynamic Mapping with Prefix:
- in: "device123/sensor/#" out: "home/sensors"
- Matches all topics under
device123/sensorand publishes them underhome/sensorswith the original subtopics appended.
- Matches all topics under
-
Exact Mapping:
- in: "monitoring/temperature" out: "metrics/temperature"
- Only
monitoring/temperatureis matched and relayed asmetrics/temperaturewith no appended subtopics.
- Only
-
Pass-Through:
- in: "alerts/#"
- Passes through all topics under
alertswithout modification.
- Passes through all topics under
brokerInUrl: URL of the input broker.brokerInOptions: Credentials for the input broker.brokerOutUrl: URL of the output broker.brokerOutOptions: Credentials for the output broker.publishOptions:retain: Whether to retain messages on the output broker.qos: QoS level for the output broker (0, 1, or 2).
debug: Set totrueto enable detailed logging.
const MqttRelay = require('mqtt-relay');
// Example configuration
const config = {
name: "relay1",
brokerInUrl: "mqtt://input-broker.example.com:1883",
brokerOutUrl: "mqtt://output-broker.example.com:1883",
topicMap: [
{ in: "device/sensor/#", out: "home/sensors" },
{ in: "alerts/#", out: "notifications/alerts" },
],
publishOptions: { retain: true, qos: 1 },
debug: true,
};
const relay = new MqttRelay(config);
relay.start();This project is licensed under the MIT License. See the LICENSE file for details.