0Pricing
WebSockets & Realtime Systems Programming · درس

بث تغييرات قاعدة البيانات إلى العملاء

ادفع تحديثات قاعدة البيانات المباشرة إلى العملاء المتصلين عبر الاستماع إلى أحداث التغيير وترحيلها عبر WebSockets في الوقت الفعلي.

بث تغييرات قاعدة البيانات إلى العملاء درس مجاني في WebSockets & Realtime Systems Programming على CoddyKit. هذا هو الدرس 3 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في WebSockets & Realtime Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة WebSockets & Realtime Systems Programming 3 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

From Polling to Pushing

Instead of clients repeatedly asking the database for new data, you can push changes the moment they happen. The backend listens to the database and relays updates over WebSockets.

Change Data Capture

Change Data Capture (CDC) is the pattern of observing inserts, updates, and deletes as a stream of events instead of querying for differences.

Database Notification Mechanisms

Many databases emit change events:

  • PostgreSQL: LISTEN / NOTIFY or logical replication
  • MongoDB: change streams
  • Firebase/Supabase: built-in realtime

Postgres LISTEN/NOTIFY

Postgres can send a notification on a channel that your backend subscribes to.

await client.query('LISTEN row_changed');

Triggering Notifications

A trigger fires pg_notify on data changes, carrying a payload describing the change.

CREATE TRIGGER notify_change
AFTER INSERT ON orders
FOR EACH ROW EXECUTE FUNCTION notify_row();

Receiving the Event

Your Node backend handles the notification and forwards it to interested WebSocket clients.

client.on('notification', (msg) => {
  broadcast(JSON.parse(msg.payload));
});

Filtering Who Gets Updates

Not every client cares about every change. Route updates by topic, user, or room so clients only receive relevant data.

function broadcast(change) {
  toRoom('orders:' + change.userId, change);
}

Sending Deltas, Not Snapshots

Transmit just what changed (a delta) rather than the whole dataset. Clients apply the delta to their local copy, saving bandwidth.

ws.send(JSON.stringify({ op: 'update', id: 42, fields: { status: 'shipped' } }));

Initial State plus Stream

A common pattern: on connect, send a full snapshot, then stream incremental changes. The client stays in sync without re-fetching everything.

Handling Reconnects

After a reconnect a client may have missed changes. Send a fresh snapshot or a cursor of changes since its last known version to resync safely.

Best Practices

Stream changes reliably:

  • Use CDC or LISTEN/NOTIFY instead of polling
  • Filter updates per client
  • Send deltas, not full snapshots
  • Resync on reconnect to fill gaps

Quick Check

Test your change-streaming knowledge.

Recap

You streamed live database changes to clients:

  • Use CDC / LISTEN-NOTIFY instead of polling
  • Relay change events over WebSockets
  • Filter per client and send deltas
  • Snapshot on connect, resync on reconnect

Your data now stays live end to end.

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

هل درس «بث تغييرات قاعدة البيانات إلى العملاء» مجاني؟

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

ماذا ستتعلم في «بث تغييرات قاعدة البيانات إلى العملاء»؟

ادفع تحديثات قاعدة البيانات المباشرة إلى العملاء المتصلين عبر الاستماع إلى أحداث التغيير وترحيلها عبر WebSockets في الوقت الفعلي. تتمرن على WebSockets & Realtime Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ WebSockets & Realtime Systems Programming؟

لا تُشترط خبرة سابقة. WebSockets & Realtime Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 3.

كم من الوقت يستغرق درس «بث تغييرات قاعدة البيانات إلى العملاء»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس WebSockets & Realtime Systems Programming هذا؟

نعم. كل درس في WebSockets & Realtime Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. WebSockets مع واجهات RESTful البرمجية
  2. الربط مع قوائم انتظار الرسائل
  3. بث تغييرات قاعدة البيانات إلى العملاء
← العودة إلى WebSockets & Realtime Systems Programming