WebSockets & Realtime Systems Programming · บทเรียน

แบ็กเพลน Pub/Sub ด้วย Redis

เชื่อมต่ออินสแตนซ์เซิร์ฟเวอร์ WebSocket หลายตัวด้วยแบ็กเพลน Redis pub/sub เพื่อให้ข้อความไปถึงไคลเอนต์ไม่ว่าจะเชื่อมต่อกับโหนดใด

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

แบ็กเพลน Pub/Sub ด้วย Redis เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Realtime Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Multi-Instance Problem

When you scale horizontally, clients connect to different server instances. A message published on node A will not reach a client connected to node B unless the nodes share it.

What a Backplane Does

A backplane is a shared channel every instance subscribes to. When one node receives a message, it publishes to the backplane, and all nodes deliver it to their local clients.

Why Redis Pub/Sub

Redis pub/sub is fast, simple, and widely available. Publishers send to a channel; all subscribers receive it instantly, making it a natural backplane.

Two Redis Connections

Redis requires separate clients for publishing and subscribing, because a subscribed connection cannot run normal commands.

const pub = redis.createClient();
const sub = redis.createClient();
await pub.connect();
await sub.connect();

Subscribing on Startup

Each instance subscribes to the shared channel when it boots.

await sub.subscribe('ws-broadcast', (message) => {
  deliverLocally(JSON.parse(message));
});

Publishing Across Nodes

Instead of broadcasting only to local sockets, publish to Redis so every node hears it.

function broadcast(payload) {
  pub.publish('ws-broadcast', JSON.stringify(payload));
}

Delivering Locally

The subscribe callback fans the message out to the clients connected to that specific instance.

function deliverLocally(payload) {
  for (const client of localClients) {
    if (client.readyState === 1) client.send(JSON.stringify(payload));
  }
}

Avoiding Echo Loops

Since the publishing node also receives its own message via subscribe, deliver only through the backplane path so each message is sent once, not twice.

Scoping by Room

For room-based apps, include the room in the payload and let each node deliver only to its local members of that room, keeping the backplane channel count small.

pub.publish('ws-broadcast', JSON.stringify({ room: 'chat-1', data }));

Limits of Redis Pub/Sub

Redis pub/sub is fire-and-forget: offline subscribers miss messages, and there is no persistence. For durability or replay, consider Redis Streams or a dedicated broker.

Best Practices

Run a healthy backplane:

  • Use separate pub and sub clients
  • Route all broadcasts through the backplane
  • Scope messages by room to reduce work
  • Know that pub/sub does not persist messages

Quick Check

Test your backplane knowledge.

Recap

You wired up a Redis pub/sub backplane:

  • Use separate publish and subscribe clients
  • Publish broadcasts to a shared channel
  • Each node delivers to its local clients
  • Scope by room and accept fire-and-forget limits

Your WebSocket app now scales across many instances.

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

เรียนรู้ WebSockets & Realtime Systems Programming ด้วย AI tutor — ฟรี

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

คอร์ส
12
บทเรียน
47

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

บทเรียน “แบ็กเพลน Pub/Sub ด้วย Redis” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “แบ็กเพลน Pub/Sub ด้วย Redis”

เชื่อมต่ออินสแตนซ์เซิร์ฟเวอร์ WebSocket หลายตัวด้วยแบ็กเพลน Redis pub/sub เพื่อให้ข้อความไปถึงไคลเอนต์ไม่ว่าจะเชื่อมต่อกับโหนดใด คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

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

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

บทเรียน “แบ็กเพลน Pub/Sub ด้วย Redis” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม

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

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

  1. กลยุทธ์การปรับขนาดแนวนอน
  2. การกระจายภาระงาน WebSockets
  3. การจัดการสถานะแบบกระจาย
  4. แบ็กเพลน Pub/Sub ด้วย Redis
← กลับไปที่ WebSockets & Realtime Systems Programming