0Pricing
WebSockets & Realtime Systems Programming · درس

وسيط Pub/Sub باستخدام Redis

صِل عدة مثيلات لخوادم WebSocket باستخدام وسيط Redis pub/sub، لتصل الرسائل إلى العملاء بصرف النظر عن العقدة التي اتصلوا بها.

وسيط Pub/Sub باستخدام Redis درس مجاني في WebSockets & Realtime Systems Programming على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.

الأسئلة الشائعة

هل درس «وسيط Pub/Sub باستخدام Redis» مجاني؟

نعم — نص درس «وسيط Pub/Sub باستخدام Redis» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebSockets & Realtime Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة WebSockets & Realtime Systems Programming 4 دروس في المجموع.

ماذا ستتعلم في «وسيط Pub/Sub باستخدام Redis»؟

صِل عدة مثيلات لخوادم WebSocket باستخدام وسيط Redis pub/sub، لتصل الرسائل إلى العملاء بصرف النظر عن العقدة التي اتصلوا بها. تتمرن على WebSockets & Realtime Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استراتيجيات التوسّع الأفقي
  2. موازنة تحميل WebSockets
  3. إدارة الحالة الموزّعة
  4. وسيط Pub/Sub باستخدام Redis
← العودة إلى WebSockets & Realtime Systems Programming