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.
Listening for Realtime Changes is a free Firebase Auth & Realtime Database Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Firebase Auth & Realtime Database Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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:
onChildAddedonChildChangedonChildRemoved
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
onValuefor single nodes - Use child events for efficient lists
- Read once with
getwhen no subscription is needed - Always detach listeners and handle errors
- Listen at the narrowest useful path
Frequently asked questions
Is the “Listening for Realtime Changes” lesson free?
Yes — the full text of “Listening for Realtime Changes” is free to read here on the web, and the Firebase Auth & Realtime Database Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Firebase Auth & Realtime Database Apps course, upgrade to CoddyKit PRO.
What will I learn in “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. You practise Firebase Auth & Realtime Database Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Firebase Auth & Realtime Database Apps?
No prior experience is required. Firebase Auth & Realtime Database Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Listening for Realtime Changes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Firebase Auth & Realtime Database Apps lesson?
Yes. Every Firebase Auth & Realtime Database Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Realtime Database Fundamentals
- Reading & Writing Data
- Structuring Your Data
- Listening for Realtime Changes