Publish/Subscribe mit Fanout-Exchanges
Lernen Sie, Nachrichten mithilfe des auf Fanout-Exchanges und temporären Queues basierenden Publish/Subscribe-Musters von RabbitMQ gleichzeitig an viele Konsumenten zu senden.
Publish/Subscribe mit Fanout-Exchanges ist eine kostenlose RabbitMQ Messaging & Async Systems-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des RabbitMQ Messaging & Async Systems-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der RabbitMQ Messaging & Async Systems-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Lerne RabbitMQ Messaging & Async Systems mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 11
- Lektionen
- 44
Häufig gestellte Fragen
Ist die Lektion „Publish/Subscribe mit Fanout-Exchanges“ kostenlos?
Ja — der vollständige Text von „Publish/Subscribe mit Fanout-Exchanges“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des RabbitMQ Messaging & Async Systems-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der RabbitMQ Messaging & Async Systems-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Publish/Subscribe mit Fanout-Exchanges“?
Lernen Sie, Nachrichten mithilfe des auf Fanout-Exchanges und temporären Queues basierenden Publish/Subscribe-Musters von RabbitMQ gleichzeitig an viele Konsumenten zu senden. Du übst RabbitMQ Messaging & Async Systems mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um RabbitMQ Messaging & Async Systems zu starten?
Keine Vorkenntnisse erforderlich. RabbitMQ Messaging & Async Systems auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Publish/Subscribe mit Fanout-Exchanges“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser RabbitMQ Messaging & Async Systems-Lektion Code schreiben und ausführen?
Ja. Jede RabbitMQ Messaging & Async Systems-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Hello World: Einfache Queue
- Work Queues: Faire Verteilung
- Nachrichtenbestätigungen und Dauerhaftigkeit
- Publish/Subscribe mit Fanout-Exchanges