RabbitMQ Messaging & Async Systems · บทเรียน

การเผยแพร่/การสมัครรับด้วย Fanout Exchange

เรียนรู้การกระจายข้อความไปยังผู้บริโภคจำนวนมากพร้อมกันด้วยรูปแบบการเผยแพร่/การสมัครรับของ RabbitMQ ซึ่งสร้างบน fanout exchange และคิวชั่วคราว

บทเรียน 4 จาก 413 ขั้นตอน

การเผยแพร่/การสมัครรับด้วย Fanout Exchange เป็นบทเรียน RabbitMQ Messaging & Async Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

เริ่มต้นได้ฟรี

เรียนรู้ RabbitMQ Messaging & Async Systems ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
11
บทเรียน
44

คำถามที่พบบ่อย

บทเรียน “การเผยแพร่/การสมัครรับด้วย Fanout Exchange” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเผยแพร่/การสมัครรับด้วย Fanout Exchange” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส RabbitMQ Messaging & Async Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเผยแพร่/การสมัครรับด้วย Fanout Exchange”

เรียนรู้การกระจายข้อความไปยังผู้บริโภคจำนวนมากพร้อมกันด้วยรูปแบบการเผยแพร่/การสมัครรับของ RabbitMQ ซึ่งสร้างบน fanout exchange และคิวชั่วคราว คุณปฏิบัติ RabbitMQ Messaging & Async Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน RabbitMQ Messaging & Async Systems หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน RabbitMQ Messaging & Async Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การเผยแพร่/การสมัครรับด้วย Fanout Exchange” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน RabbitMQ Messaging & Async Systems นี้ได้ไหม

ได้ บทเรียน RabbitMQ Messaging & Async Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. สวัสดีชาวโลก: คิวอย่างง่าย
  2. คิวงาน: การกระจายงานอย่างเป็นธรรม
  3. การยืนยันข้อความและความคงทน
  4. การเผยแพร่/การสมัครรับด้วย Fanout Exchange
← กลับไปที่ RabbitMQ Messaging & Async Systems