- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
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:
Let’s dive in!
Prerequisites
Before you start, make sure you have the following installed:
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:
Log in with:
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:
Youtube :
You’ve now set up:
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.
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!
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:
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
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
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
GitHub Link:
Youtube :
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.