0Pricing
Firebase Auth & Realtime Database Apps · บทเรียน

การรับฟังการเปลี่ยนแปลงแบบเรียลไทม์

ทำให้แอปเป็นแบบเรียลไทม์อย่างแท้จริงด้วยการสมัครรับเหตุการณ์จาก Realtime Database ตอบสนองต่อการเปลี่ยนแปลงของค่าและรายการลูกขณะที่เกิดขึ้น และล้างตัวรับฟังเพื่อป้องกันหน่วยความจำรั่ว

การรับฟังการเปลี่ยนแปลงแบบเรียลไทม์ เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

From Fetch to Stream

Reading data once gives you a snapshot. The power of Realtime Database is live updates: your app reacts the instant data changes on the server, with no polling.

This is done by attaching listeners to a location in the database.

The Value Event

The most common listener is value. It fires once immediately with current data, then again every time anything under that location changes.

import { getDatabase, ref, onValue } from 'firebase/database';

const db = getDatabase();
onValue(ref(db, 'scores/global'), (snapshot) => {
  console.log('New value:', snapshot.val());
});

Reading the Snapshot

The callback receives a DataSnapshot. Use .val() for the data, .exists() to check presence, and .key for the node name.

onValue(ref(db, 'user/42'), (snap) => {
  if (snap.exists()) {
    console.log(snap.key, snap.val());
  }
});

Child Events

For lists, value events resend the whole node on every change. Child events are more efficient, firing per item:

  • onChildAdded
  • onChildChanged
  • onChildRemoved

Using onChildAdded

onChildAdded fires once for each existing child, then for every new one. It is perfect for chat messages or feeds.

import { onChildAdded } from 'firebase/database';

onChildAdded(ref(db, 'messages'), (snap) => {
  appendMessage(snap.key, snap.val());
});

Reacting to Changes and Removals

Pair additions with change and removal handlers to keep your UI in sync without re-rendering everything.

import { onChildChanged, onChildRemoved } from 'firebase/database';

onChildChanged(ref(db, 'messages'), s => updateMessage(s.key, s.val()));
onChildRemoved(ref(db, 'messages'), s => removeMessage(s.key));

Reading Once

Sometimes you want current data without an ongoing subscription. Use get for a one-time read instead of a listener.

import { get } from 'firebase/database';

const snap = await get(ref(db, 'config'));
console.log(snap.val());

Detaching Listeners

Listeners stay active until removed, consuming bandwidth and memory. Always detach them when a screen unmounts, using the unsubscribe function returned by onValue.

const unsubscribe = onValue(ref(db, 'scores'), cb);
// later, when leaving the screen:
unsubscribe();

Handling Errors

Listeners can fail, often due to Security Rules denying read access. Provide an error callback so failures are visible, not silent.

onValue(ref(db, 'private'), (snap) => render(snap), (error) => {
  console.error('Read failed:', error.message);
});

Performance Tips

Keep listeners efficient:

  • Listen at the smallest path that has the data you need
  • Prefer child events for large lists
  • Never attach a value listener to the database root

A Live Counter Example

Putting it together: attach a value listener to a counter node, update the DOM in the callback, and unsubscribe on cleanup. The number changes for every connected client in real time.

const unsub = onValue(ref(db, 'visitors/count'), (snap) => {
  document.getElementById('count').textContent = snap.val() ?? 0;
});

Quick Check

Test your understanding of realtime listeners.

Recap

Your app can now respond to data the moment it changes.

  • Use onValue for single nodes
  • Use child events for efficient lists
  • Read once with get when no subscription is needed
  • Always detach listeners and handle errors
  • Listen at the narrowest useful path

คำถามที่พบบ่อย

บทเรียน “การรับฟังการเปลี่ยนแปลงแบบเรียลไทม์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การรับฟังการเปลี่ยนแปลงแบบเรียลไทม์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การรับฟังการเปลี่ยนแปลงแบบเรียลไทม์”

ทำให้แอปเป็นแบบเรียลไทม์อย่างแท้จริงด้วยการสมัครรับเหตุการณ์จาก Realtime Database ตอบสนองต่อการเปลี่ยนแปลงของค่าและรายการลูกขณะที่เกิดขึ้น และล้างตัวรับฟังเพื่อป้องกันหน่วยความจำรั่ว คุณปฏิบัติ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานฐานข้อมูลแบบเรียลไทม์
  2. การอ่านและเขียนข้อมูล
  3. การจัดโครงสร้างข้อมูล
  4. การรับฟังการเปลี่ยนแปลงแบบเรียลไทม์
← กลับไปที่ Firebase Auth & Realtime Database Apps