قنوات الحضور والبث
تجاوز تغييرات الجداول باستخدام Supabase Realtime Presence لتتبع المتصلين، وBroadcast لإرسال رسائل مؤقتة بين العملاء.
قنوات الحضور والبث درس مجاني في Supabase Backend as a Service على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Supabase Backend as a Service، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Supabase Backend as a Service 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two More Realtime Features
Besides Postgres change streams, Supabase Realtime offers Presence (track online users) and Broadcast (send transient messages). Neither requires a database table.
What Is Presence?
Presence lets each client share small state (like a username or cursor position) and see everyone else's state in the same channel in real time.
Creating a Channel
A channel is a named room clients join.
const room = supabase.channel('room-1', {
config: { presence: { key: 'user-42' } }
});Tracking Presence
After subscribing, call track to publish your state to everyone in the channel.
room.subscribe(async (status) => {
if (status === 'SUBSCRIBED') {
await room.track({ name: 'Ada', online_at: Date.now() });
}
});Reading Who Is Online
Listen for sync events and read the current presence state of all members.
room.on('presence', { event: 'sync' }, () => {
const state = room.presenceState();
console.log('Online:', Object.keys(state).length);
});Join and Leave Events
Presence emits join and leave events so you can animate users appearing and disappearing.
room.on('presence', { event: 'join' }, ({ newPresences }) => {
console.log('Joined:', newPresences.length);
});What Is Broadcast?
Broadcast sends low-latency messages to everyone in a channel without storing them. Great for cursors, typing indicators, and game moves.
Sending a Broadcast
Use send with a custom event name and payload.
room.send({
type: 'broadcast',
event: 'cursor',
payload: { x: 120, y: 80 }
});Receiving a Broadcast
Subscribe to the same event name to handle incoming messages.
room.on('broadcast', { event: 'cursor' }, ({ payload }) => {
console.log('Cursor at', payload.x, payload.y);
});Cleaning Up
Always remove channels when a component unmounts to free connections.
function cleanup(channel, supabase) {
supabase.removeChannel(channel);
return 'removed';
}
console.log(cleanup('chan', { removeChannel: () => {} }));Choosing the Right Tool
Use Postgres changes for persisted data, Presence for online status, and Broadcast for fast disposable messages.
Quick Check
Test your understanding of Presence and Broadcast.
Recap
You learned to track online users with Presence and send ephemeral messages with Broadcast, plus when to use each versus Postgres change streams, and to clean up channels.
الأسئلة الشائعة
هل درس «قنوات الحضور والبث» مجاني؟
نعم — نص درس «قنوات الحضور والبث» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Supabase Backend as a Service، انتقل إلى CoddyKit PRO. تتضمن دورة Supabase Backend as a Service 4 دروس في المجموع.
ماذا ستتعلم في «قنوات الحضور والبث»؟
تجاوز تغييرات الجداول باستخدام Supabase Realtime Presence لتتبع المتصلين، وBroadcast لإرسال رسائل مؤقتة بين العملاء. تتمرن على Supabase Backend as a Service مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Supabase Backend as a Service؟
لا تُشترط خبرة سابقة. Supabase Backend as a Service على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «قنوات الحضور والبث»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Supabase Backend as a Service هذا؟
نعم. كل درس في Supabase Backend as a Service يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.