mqtt-relay
v0.0.9
Published
A flexible MQTT relay module for forwarding messages between brokers.
Downloads
490
Maintainers
Readme
mqtt-relay
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.
Installation
Install the mqtt-relay
package:
npm install mqtt-relay
Usage
Basic Setup
After installation, set up a configuration file to specify the MQTT brokers, relay options, and topic mappings. You can find a sample configuration file in node_modules/mqtt-relay/relay-config.sample.yaml
. Copy it to your project root and customize it:
cp node_modules/mqtt-relay/relay-config.sample.yaml ./relay-config.yaml
To run the relay with this configuration:
node mqtt-relay.js relay-config.yaml
You can run multiple instances with different configurations. For each instance, specify the config file as a parameter:
node mqtt-relay.js another-config.yaml
Using PM2
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" start mqtt-relay.js -- relay-config.yaml
Configuration
Config Structure
The configuration file allows you to define both input and output brokers, publishing options, and detailed topic mappings for flexible topic transformations.
Example Configuration
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
Configuration
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.
Types of Mappings
Prefix Match with Dynamic Subtopics:
- If the
in
topic 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
in
topic 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
out
Specified):- If no
out
field 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
Constructing topicMap
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/sensor
and publishes them underhome/sensors
with the original subtopics appended.
- Matches all topics under
Exact Mapping:
- in: "monitoring/temperature" out: "metrics/temperature"
- Only
monitoring/temperature
is matched and relayed asmetrics/temperature
with no appended subtopics.
- Only
Pass-Through:
- in: "alerts/#"
- Passes through all topics under
alerts
without modification.
- Passes through all topics under
Additional Options
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 totrue
to enable detailed logging.
License
This project is licensed under the MIT License. See the LICENSE file for details.