0Pricing
RabbitMQ Messaging & Async Systems · Lezione

Publish/subscribe con gli exchange fanout

Impari a trasmettere messaggi contemporaneamente a molti consumatori usando il pattern publish/subscribe di RabbitMQ basato su exchange fanout e code temporanee.

Publish/subscribe con gli exchange fanout è una lezione RabbitMQ Messaging & Async Systems gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento RabbitMQ Messaging & Async Systems, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso RabbitMQ Messaging & Async Systems include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Publish/subscribe con gli exchange fanout» è gratuita?

Sì — il testo completo di «Publish/subscribe con gli exchange fanout» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso RabbitMQ Messaging & Async Systems, passa a CoddyKit PRO. Il corso RabbitMQ Messaging & Async Systems include 4 lezioni in totale.

Cosa imparerò in «Publish/subscribe con gli exchange fanout»?

Impari a trasmettere messaggi contemporaneamente a molti consumatori usando il pattern publish/subscribe di RabbitMQ basato su exchange fanout e code temporanee. Eserciti RabbitMQ Messaging & Async Systems con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare RabbitMQ Messaging & Async Systems?

Non è richiesta alcuna esperienza precedente. RabbitMQ Messaging & Async Systems su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Publish/subscribe con gli exchange fanout»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione RabbitMQ Messaging & Async Systems?

Sì. Ogni lezione RabbitMQ Messaging & Async Systems include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Hello World: coda semplice
  2. Work queue: distribuzione equa
  3. Conferme e durabilità dei messaggi
  4. Publish/subscribe con gli exchange fanout
← Torna a RabbitMQ Messaging & Async Systems