ช่องทางแสดงสถานะและกระจายข้อความ
ก้าวไปไกลกว่าการติดตามการเปลี่ยนแปลงของตารางด้วย Supabase Realtime Presence เพื่อติดตามว่าใครกำลังออนไลน์ และ Broadcast เพื่อส่งข้อความชั่วคราวระหว่างไคลเอ็นต์
ช่องทางแสดงสถานะและกระจายข้อความ เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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.
เรียนรู้ Supabase Backend as a Service ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 11
- บทเรียน
- 40
คำถามที่พบบ่อย
บทเรียน “ช่องทางแสดงสถานะและกระจายข้อความ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ช่องทางแสดงสถานะและกระจายข้อความ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ช่องทางแสดงสถานะและกระจายข้อความ”
ก้าวไปไกลกว่าการติดตามการเปลี่ยนแปลงของตารางด้วย Supabase Realtime Presence เพื่อติดตามว่าใครกำลังออนไลน์ และ Broadcast เพื่อส่งข้อความชั่วคราวระหว่างไคลเอ็นต์ คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจการสมัครรับข้อมูลแบบเรียลไทม์
- การสมัครรับการเปลี่ยนแปลงของตาราง
- การสร้างฟีเจอร์แชตสด
- ช่องทางแสดงสถานะและกระจายข้อความ