การตรวจจับสถานะการออนไลน์และการมีอยู่
สร้างตัวบ่งชี้ออนไลน์และออฟไลน์แบบเรียลไทม์ด้วย presence ของ Realtime Database ตัวจัดการ onDisconnect และโหนดพิเศษ .info/connected เพื่อสะท้อนสถานะการเชื่อมต่อได้อย่างน่าเชื่อถือ
การตรวจจับสถานะการออนไลน์และการมีอยู่ เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What Is Presence
Presence means knowing which users are currently online. Chat apps, collaborative tools, and games all rely on it to show 'active now' indicators.
Realtime Database has special features that make presence robust even when a client disconnects unexpectedly.
The Connection Challenge
The hard part is the ungraceful disconnect: a user closes their laptop or loses signal. The app never gets to set status to 'offline'. We need the server to do it for us.
The .info/connected Node
Firebase exposes a special read-only boolean at .info/connected that is true when the client has a live connection and false when it does not.
import { getDatabase, ref, onValue } from 'firebase/database';
const db = getDatabase();
onValue(ref(db, '.info/connected'), (snap) => {
console.log('Connected?', snap.val() === true);
});onDisconnect Basics
onDisconnect registers an action that the server performs when the client disconnects. It is the key to reliable presence.
import { onDisconnect, set } from 'firebase/database';
const statusRef = ref(db, 'status/' + uid);
onDisconnect(statusRef).set({ state: 'offline' });Order of Operations
The correct sequence is critical:
- Wait until
.info/connectedis true - Register the
onDisconnectaction first - Only then set status to online
This guarantees the offline handler is armed before you go online.
A Complete Presence Setup
Combining the pieces gives a reliable presence system.
onValue(ref(db, '.info/connected'), (snap) => {
if (snap.val() !== true) return;
onDisconnect(statusRef).set({ state: 'offline', at: Date.now() })
.then(() => set(statusRef, { state: 'online', at: Date.now() }));
});Server Timestamps
Use serverTimestamp() instead of the client clock so 'last seen' times are consistent across devices with skewed clocks.
import { serverTimestamp } from 'firebase/database';
set(statusRef, { state: 'online', lastChanged: serverTimestamp() });Multiple Devices
A user may be online on phone and laptop at once. Track presence per connection (a unique key per session) and treat the user as offline only when all connections drop.
import { push } from 'firebase/database';
const conRef = push(ref(db, 'status/' + uid + '/connections'));
onDisconnect(conRef).remove();Cancelling onDisconnect
If the user logs out cleanly, cancel the pending onDisconnect action and set offline yourself, so stale handlers do not fire later.
import { onDisconnect } from 'firebase/database';
await onDisconnect(statusRef).cancel();
await set(statusRef, { state: 'offline' });Displaying Presence
Other clients simply listen to the status node. A green dot appears when state is online and disappears when the server flips it to offline on disconnect.
onValue(ref(db, 'status/' + otherUid), (snap) => {
const online = snap.val()?.state === 'online';
toggleDot(online);
});Best Practices
Keep presence reliable and cheap:
- Store presence in a shallow, dedicated node
- Use server timestamps for last-seen
- Account for multiple connections
- Always register onDisconnect before going online
Quick Check
Test your understanding of presence.
Recap
You can now build reliable online indicators.
- Watch
.info/connectedfor live connection state - Use
onDisconnectso the server marks users offline - Register onDisconnect before going online
- Use server timestamps and per-connection tracking
- Cancel handlers on clean logout
เรียนรู้ Firebase Auth & Realtime Database Apps ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 11
- บทเรียน
- 44
คำถามที่พบบ่อย
บทเรียน “การตรวจจับสถานะการออนไลน์และการมีอยู่” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจจับสถานะการออนไลน์และการมีอยู่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจจับสถานะการออนไลน์และการมีอยู่”
สร้างตัวบ่งชี้ออนไลน์และออฟไลน์แบบเรียลไทม์ด้วย presence ของ Realtime Database ตัวจัดการ onDisconnect และโหนดพิเศษ .info/connected เพื่อสะท้อนสถานะการเชื่อมต่อได้อย่างน่าเชื่อถือ คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจจับสถานะการออนไลน์และการมีอยู่” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม
ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเปิดใช้การเก็บข้อมูลแบบออฟไลน์
- การจัดการการตัดการเชื่อมต่อเครือข่าย
- แนวทางการซิงโครไนซ์ข้อมูล
- การตรวจจับสถานะการออนไลน์และการมีอยู่