0Pricing
RabbitMQ Messaging & Async Systems · Lekcja

Publish/subscribe z exchange typu fanout

Dowiedz się, jak jednocześnie rozgłaszać komunikaty do wielu konsumentów za pomocą wzorca publish/subscribe RabbitMQ opartego na exchange typu fanout i tymczasowych kolejkach.

Publish/subscribe z exchange typu fanout to bezpłatna lekcja RabbitMQ Messaging & Async Systems na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej RabbitMQ Messaging & Async Systems, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs RabbitMQ Messaging & Async Systems zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Publish/subscribe z exchange typu fanout” jest bezpłatna?

Tak — pełny tekst „Publish/subscribe z exchange typu fanout” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu RabbitMQ Messaging & Async Systems, przejdź na CoddyKit PRO. Kurs RabbitMQ Messaging & Async Systems zawiera 4 lekcji w sumie.

Co nauczysz się w „Publish/subscribe z exchange typu fanout”?

Dowiedz się, jak jednocześnie rozgłaszać komunikaty do wielu konsumentów za pomocą wzorca publish/subscribe RabbitMQ opartego na exchange typu fanout i tymczasowych kolejkach. Ćwiczysz RabbitMQ Messaging & Async Systems z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć RabbitMQ Messaging & Async Systems?

Nie wymagamy żadnego doświadczenia. RabbitMQ Messaging & Async Systems w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Publish/subscribe z exchange typu fanout”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji RabbitMQ Messaging & Async Systems?

Tak. Każda lekcja RabbitMQ Messaging & Async Systems zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Hello World: prosta kolejka
  2. Kolejki zadań: sprawiedliwy przydział
  3. Potwierdzenia komunikatów i trwałość
  4. Publish/subscribe z exchange typu fanout
← Powrót do RabbitMQ Messaging & Async Systems