Listening for Realtime Changes
Make your app truly live by subscribing to Realtime Database events, reacting to value and child changes as they happen, and cleaning up listeners to avoid leaks.
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());
});All lessons in this course
- Realtime Database Fundamentals
- Reading & Writing Data
- Structuring Your Data
- Listening for Realtime Changes