Publish MQTT messages from broker to NATS or Kafka
APACHE-2.0 License
The MQTT Relay is a service that listens to MQTT topics from a broker and publishes the received messages to a queue system like NATS or Kafka. This project provides a modular and extensible solution for message relaying and transformation between different messaging systems.
It was written to solve an IoT use case like below, where there is a huge influx of messages (or metrics) being published through MQTT and the backend services may not be able to handle them all (due to database constraints). MQTT Relay acts as a queue source providing authentication and buffering the messages temporarily.
Follow these steps to set up and run the MQTT Relay.
Clone the Repository
git clone https://github.com/yourusername/mqtt-relay.git
cd mqtt-relay
Install Dependencies
go mod tidy
Create a configuration file named config/config.yaml
. Here’s a sample configuration:
mqtt:
broker: "tcp://broker.hivemq.com:1883"
clientID: "mqtt_relay"
username: "your_mqtt_username"
password: "your_mqtt_password"
caCertFile: "path/to/ca.crt"
clientCertFile: "path/to/client.crt"
clientKeyFile: "path/to/client.key"
topics:
- mqttTopic: "topic/one"
queueTopic: "queue_topic_one"
- mqttTopic: "topic/two"
queueTopic: "queue_topic_two"
- mqttTopic: "topic/three"
queueTopic: "queue_topic_three"
nats:
url: "tls://your_nats_server:4222"
username: "your_nats_username"
password: "your_nats_password"
caCertFile: "path/to/nats/ca.crt"
Build the Application
go build -o mqtt-relay
Run the Application
./mqtt-relay
The application will start, connect to the MQTT broker, subscribe to the specified topics, and publish received messages to the configured queue system.
You can run NATS and Kafka using Docker if you don’t have them set up.
Run NATS
docker run -d --name nats -p 4222:4222 nats:latest
Run Kafka
docker run -d --name kafka -p 9092:9092 -e KAFKA_ADVERTISED_LISTENERS=INSIDE://kafka:9092,OUTSIDE://localhost:9092 -e KAFKA_LISTENER_NAME=INSIDE -e KAFKA_LISTENERS=INSIDE://0.0.0.0:9092,OUTSIDE://0.0.0.0:9092 wurstmeister/kafka:latest
To test the application, ensure that both the MQTT broker and the queue system (NATS or Kafka) are running. The application should start receiving messages from the MQTT broker and publishing them to the queue.
Logs are output to the standard console. You can add or modify logging in the source code to adjust verbosity or format.
This project is licensed under the MIT License. See the LICENSE file for details.
For any issues or questions, please open an issue on GitHub or contact [email protected].