RabbitMQ Messaging & Async Systems · レッスン

Fanout ExchangeによるPublish/Subscribe

fanout exchangeと一時キューを基盤とするRabbitMQのPublish/Subscribeパターンを使い、多数のコンシューマーへ同時にメッセージをブロードキャストする方法を学びます。

レッスン 4/413 ステップ

「Fanout ExchangeによるPublish/Subscribe」はCoddyKit上の無料RabbitMQ Messaging & Async Systemsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはRabbitMQ Messaging & Async Systems学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 RabbitMQ Messaging & Async Systemsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.

無料で開始

AI チューターと学ぶ RabbitMQ Messaging & Async Systems — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
11
レッスン
44

よくある質問

「Fanout ExchangeによるPublish/Subscribe」レッスンは無料ですか?

はい。「Fanout ExchangeによるPublish/Subscribe」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、RabbitMQ Messaging & Async Systemsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 RabbitMQ Messaging & Async Systemsコースには全4レッスンが含まれています。

「Fanout ExchangeによるPublish/Subscribe」で何を学びますか?

fanout exchangeと一時キューを基盤とするRabbitMQのPublish/Subscribeパターンを使い、多数のコンシューマーへ同時にメッセージをブロードキャストする方法を学びます。 ブラウザで直接実行するハンズオンコードでRabbitMQ Messaging & Async Systemsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

RabbitMQ Messaging & Async Systemsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのRabbitMQ Messaging & Async Systemsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「Fanout ExchangeによるPublish/Subscribe」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このRabbitMQ Messaging & Async Systemsレッスンでコードを書いて実行できますか?

はい。すべてのRabbitMQ Messaging & Async Systemsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Hello World:シンプルなキュー
  2. Work Queues:公平なディスパッチ
  3. メッセージの確認応答と永続性
  4. Fanout ExchangeによるPublish/Subscribe
← RabbitMQ Messaging & Async Systemsに戻る