0
0
RabbitMQdevops~5 mins

Publish-subscribe for broadcasting in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to send the same message to many receivers at once. Publish-subscribe helps by letting one sender broadcast messages to multiple listeners easily.
When you want to send notifications to many services at the same time.
When multiple apps need to react to the same event, like a user signup.
When you want to separate message producers from consumers for better scaling.
When you want to add new listeners without changing the sender.
When you want to broadcast logs or metrics to different monitoring tools.
Config File - publish_subscribe_setup.sh
publish_subscribe_setup.sh
#!/bin/bash
# Create a fanout exchange for broadcasting
rabbitmqadmin declare exchange name=broadcast_exchange type=fanout

# Create queues for different consumers
rabbitmqadmin declare queue name=queue_1 durable=true
rabbitmqadmin declare queue name=queue_2 durable=true

# Bind queues to the exchange
rabbitmqadmin declare binding source=broadcast_exchange destination=queue_1
rabbitmqadmin declare binding source=broadcast_exchange destination=queue_2

This script sets up a fanout exchange named broadcast_exchange which broadcasts messages to all bound queues. It creates two durable queues queue_1 and queue_2. Then it binds both queues to the exchange so they receive all messages sent to it.

Commands
This command creates a fanout exchange named 'broadcast_exchange' which will broadcast messages to all queues bound to it.
Terminal
rabbitmqadmin declare exchange name=broadcast_exchange type=fanout
Expected OutputExpected
Successfully declared exchange 'broadcast_exchange'
name - Sets the name of the exchange
type - Defines the exchange type; 'fanout' broadcasts to all bound queues
Creates a durable queue named 'queue_1' that will receive broadcast messages.
Terminal
rabbitmqadmin declare queue name=queue_1 durable=true
Expected OutputExpected
Successfully declared queue 'queue_1'
name - Sets the queue name
durable - Makes the queue survive RabbitMQ restarts
Creates another durable queue named 'queue_2' for receiving broadcast messages.
Terminal
rabbitmqadmin declare queue name=queue_2 durable=true
Expected OutputExpected
Successfully declared queue 'queue_2'
name - Sets the queue name
durable - Makes the queue survive RabbitMQ restarts
Binds 'queue_1' to the 'broadcast_exchange' so it receives all messages sent to the exchange.
Terminal
rabbitmqadmin declare binding source=broadcast_exchange destination=queue_1
Expected OutputExpected
Successfully declared binding from 'broadcast_exchange' to 'queue_1'
source - The exchange name
destination - The queue name to bind
Binds 'queue_2' to the 'broadcast_exchange' so it also receives all broadcast messages.
Terminal
rabbitmqadmin declare binding source=broadcast_exchange destination=queue_2
Expected OutputExpected
Successfully declared binding from 'broadcast_exchange' to 'queue_2'
source - The exchange name
destination - The queue name to bind
Key Concept

If you remember nothing else from this pattern, remember: a fanout exchange sends every message to all queues bound to it, enabling easy broadcasting.

Common Mistakes
Using a direct or topic exchange instead of fanout for broadcasting
Direct and topic exchanges route messages based on keys, so not all queues get the message.
Use a fanout exchange to broadcast messages to all bound queues regardless of routing keys.
Not binding queues to the exchange after creating them
Queues won't receive any messages if they are not bound to the exchange.
Always bind each queue to the fanout exchange to receive broadcast messages.
Creating non-durable queues without understanding persistence needs
Non-durable queues disappear after RabbitMQ restarts, losing messages.
Set queues as durable if you want them to survive server restarts.
Summary
Create a fanout exchange to broadcast messages to multiple queues.
Create durable queues that will receive the broadcast messages.
Bind each queue to the fanout exchange to ensure they get all messages.