RabbitMQ is a powerful message broker that supports multiple protocols, including AMQP and MQTT. If youāre building event-driven microservices or IoT-based apps, having RabbitMQ locally with MQTT support is extremely useful.
In this guide, youāll learn how to:
- Spin up RabbitMQ using Docker
- Enable the Management UI
- Add MQTT protocol support
- Test both MQTT and AMQP pub/sub functionality via terminal scripts
Letās dive in!
ā Prerequisites
Before you start, make sure you have the following installed:
- Docker
- Docker Compose
-
jq
for parsing JSON (used in the subscriber script)
Install jq
on Ubuntu:
sudo apt install jq
š§± Step 1: Docker ComposeĀ Setup
Create a file called rabbitmq-docker-compose.yml
and paste in the following configuration:
services:
# RabbitMQ
rabbitmq:
image: rabbitmq:4-management
container_name: rabbitmq
restart: unless-stopped
ports:
- "15672:15672" # RabbitMQ Management UI
- "5672:5672" # RabbitMQ AMQP
environment:
RABBITMQ_HOST: '%'
RABBITMQ_DEFAULT_USER: user
RABBITMQ_DEFAULT_PASS: root
volumes:
- rabbitmq_data:/var/lib/rabbitmq
networks:
- rabbitmq_network
healthcheck:
test:
- CMD
- rabbitmq-diagnostics
- '-q'
- ping
retries: 3
timeout: 5s
interval: 30s
volumes:
rabbitmq_data:
driver: local
networks:
rabbitmq_network:
driver: bridge
š Step 2: Launch the RabbitMQ Container
Run the following command to spin up the service:
docker compose -f rabbitmq-docker-compose.yml up -d --build
Verify that itās running:
docker ps
You should see the RabbitMQ container up and listening on ports 5672, 15672, and 1883.
š Step 3: Access the RabbitMQ Management UI
Open your browser and visit:
http://localhost:15672
Log in with:
-
Username:
user
-
Password:
root
Youāll get a full dashboard with queues, exchanges, bindings, and users.
š Step 4: Enable MQTTĀ Plugin
RabbitMQ supports MQTT through its plugin system. To enable it:
docker exec -it rabbitmq rabbitmq-plugins enable rabbitmq_mqtt
Verify that itās enabled:
docker exec -it rabbitmq rabbitmq-plugins list | grep mqtt
You should see output like this:
[E*] rabbitmq_mqtt
š Step 5: Test MQTT with Mosquitto
ā Install Mosquitto Clients
On Ubuntu:
sudo apt install mosquitto-clients
After installation. Open a new terminal and type the command to publish a message
š¤ Publish a Message
mosquitto_pub -h localhost -p 1883 -t test_mqtt -m "Hello, MQTT!" -u user -P root
š„ Subscribe to aĀ Topic
In a second terminal:
mosquitto_sub -h localhost -p 1883 -t test_mqtt -u user -P root
You should see:
Hello, MQTT!
š„ļø Step 6: Test AMQP Pub/Sub (Optional BashĀ Scripts)
You can also test RabbitMQās core AMQP protocol using publisher.sh
and subscriber.sh
scripts.
š Sample publisher.sh
#!/bin/bash
RABBITMQ_USER="user"
RABBITMQ_PASS="root"
RABBITMQ_HOST="localhost"
QUEUE_NAME="test_queue"
# Declare the queue
curl -u "$RABBITMQ_USER:$RABBITMQ_PASS" -X PUT \
-H "Content-Type: application/json" \
-d '{"auto_delete":false,"durable":true}' \
"http://$RABBITMQ_HOST:15672/api/queues/%2F/$QUEUE_NAME"
# Publish messages every 5 seconds
while true; do
MESSAGE="Hello, RabbitMQ, This is a message from docker compose! $(date)"
curl -u "$RABBITMQ_USER:$RABBITMQ_PASS" -X POST \
-H "Content-Type: application/json" \
-d "{\"properties\":{},\"routing_key\":\"$QUEUE_NAME\",\"payload\":\"$MESSAGE\",\"payload_encoding\":\"string\"}" \
"http://$RABBITMQ_HOST:15672/api/exchanges/%2F/amq.default/publish"
echo " [x] Sent '$MESSAGE'"
sleep 5
done
š Sample subscriber.sh
#!/bin/bash
RABBITMQ_USER="user"
RABBITMQ_PASS="root"
RABBITMQ_HOST="localhost"
QUEUE_NAME="test_queue"
# Continuously fetch messages from the queue
while true; do
RESPONSE=$(curl -u "$RABBITMQ_USER:$RABBITMQ_PASS" -X POST \
-H "Content-Type: application/json" \
-d '{"count":1,"ackmode":"ack_requeue_false","encoding":"auto","truncate":50000}' \
"http://$RABBITMQ_HOST:15672/api/queues/%2F/$QUEUE_NAME/get")
echo "Response: $RESPONSE"
MESSAGE=$(echo "$RESPONSE" | jq -r '.[0].payload' 2>/dev/null)
if [ "$MESSAGE" != "null" ] && [ -n "$MESSAGE" ]; then
echo " [x] Received '$MESSAGE'"
fi
sleep 1
done
ā¶ļø Make Scripts Executable
chmod +x publisher.sh subscriber.sh
Then run them in separate terminals:
./publisher.sh
./subscriber.sh
š§Æ Step 7: Stop theĀ Service
When youāre done testing:
docker compose -f rabbitmq-docker-compose.yml down
ā Summary
GitHub Link: https://github.com/rafi021/rabbitmq-message-broker-docker-compose
Youtube : https://youtu.be/Wld_qudiTeo
Youāve now set up:
- RabbitMQ with MQTT and AMQP support
- A web-based Management UI
- Pub/Sub testing with both MQTT clients and bash scripts
This local setup is great for developing event-driven backends, IoT systems, or microservices without relying on cloud infrastructure.
If you found this helpful, consider bookmarking or sharing it with your team.
Top comments (0)