Publish/Subscribe with Fanout Exchanges
Learn how to broadcast messages to many consumers at once using RabbitMQ's publish/subscribe pattern built on fanout exchanges and temporary queues.
Publish/Subscribe with Fanout Exchanges is a free RabbitMQ Messaging & Async Systems lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the RabbitMQ Messaging & Async Systems learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond a Single Consumer
Work queues deliver each message to one worker. But sometimes every interested consumer needs the same message — like broadcasting a live score to all dashboards.
This is the publish/subscribe pattern.
The Fanout Exchange
Pub/sub in RabbitMQ uses a fanout exchange, which copies every incoming message to all queues bound to it, ignoring routing keys entirely.
Declaring a Fanout Exchange
A producer declares the fanout exchange and publishes to it.
channel.exchangeDeclare('scores', 'fanout');
channel.basicPublish('scores', '', null, body);Empty Routing Key
Notice the routing key is an empty string. Fanout exchanges ignore it, so producers do not need to specify any routing logic at all.
Temporary Queues
Each subscriber usually wants a fresh, private queue. Declaring a queue with no name lets RabbitMQ generate a random one, and marking it exclusive auto-deletes it when the consumer disconnects.
String q = channel.queueDeclare().getQueue();Binding the Queue
The subscriber binds its temporary queue to the fanout exchange so it begins receiving broadcasts.
channel.queueBind(q, 'scores', '');Multiple Independent Subscribers
Because each subscriber has its own queue, they all get independent copies of every message. Adding a new subscriber does not affect the others.
Pub/Sub vs Work Queues
Key difference:
- Work queue — one queue, many workers, each message handled once
- Pub/sub — many queues, each subscriber gets every message
Common Use Cases
Fanout pub/sub fits:
- Live notifications and dashboards
- Cache invalidation across many nodes
- Broadcasting configuration changes
- Logging to multiple sinks
Messages Without Consumers
If no queue is bound to a fanout exchange when a message arrives, the message is simply discarded. Fanout does not store anything itself — queues do.
Scaling Subscribers
To scale pub/sub, just add more subscribers, each with its own temporary queue bound to the exchange. The fanout exchange handles fan-out automatically.
Quick Check
Test your pub/sub knowledge.
Recap
You learned the publish/subscribe pattern:
- Fanout exchanges broadcast to all bound queues
- The routing key is ignored
- Subscribers use temporary exclusive queues
- Each subscriber gets an independent copy of every message
This pattern is the foundation for notifications, cache invalidation, and live updates.
Frequently asked questions
Is the “Publish/Subscribe with Fanout Exchanges” lesson free?
Yes — the full text of “Publish/Subscribe with Fanout Exchanges” is free to read here on the web, and the RabbitMQ Messaging & Async Systems course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the RabbitMQ Messaging & Async Systems course, upgrade to CoddyKit PRO.
What will I learn in “Publish/Subscribe with Fanout Exchanges”?
Learn how to broadcast messages to many consumers at once using RabbitMQ's publish/subscribe pattern built on fanout exchanges and temporary queues. You practise RabbitMQ Messaging & Async Systems with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start RabbitMQ Messaging & Async Systems?
No prior experience is required. RabbitMQ Messaging & Async Systems on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Publish/Subscribe with Fanout Exchanges” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this RabbitMQ Messaging & Async Systems lesson?
Yes. Every RabbitMQ Messaging & Async Systems lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Hello World: Simple Queue
- Work Queues: Fair Dispatch
- Message Acknowledgements & Durability
- Publish/Subscribe with Fanout Exchanges