6. Realtime Database: Low-Latency JSON Storage
Building Live Applications Without Polling
Firebase Realtime Database is a low-latency, JSON-based database that synchronizes data in real-time across all connected clients. Instead of polling an endpoint every few seconds, clients subscribe to changes and receive them instantly as they happen — sub-100ms latency from database write to client delivery.
Realtime Database is built on WebSockets for persistent connections. All data is stored as a single JSON tree, which means it's ideal for small, nested data structures. Unlike Firestore, Realtime Database doesn't support queries with filters and sorting. You retrieve entire nodes or specific paths. This makes it perfect for real-time features like chat, live dashboards, and collaborative editing. Realtime Database is optimized for low latency and high throughput. Understanding Realtime Database's strengths and limitations is essential for choosing the right database for your use case.
The JSON Tree Structure
Realtime Database organizes data as a single JSON tree. This means all data is nested under a single root. You can reference any path in the tree. For example, /users/alice/name. This structure is simple but has limitations. Deeply nested data can be slow to retrieve. The entire node is returned when you read a path. You can't query across different branches efficiently. This structure is best for small, interconnected data. Design your tree to minimize nesting depth. Use flat structures with references where possible. Understanding the tree structure is essential for designing efficient Realtime Database applications.
import { getDatabase, ref, set, update, push, onValue } from 'firebase/database'; const db = getDatabase(); // Write data set(ref(db, 'users/alice'), { name: 'Alice', email: 'alice@example.com' }); // Update specific fields update(ref(db, 'users/alice'), { name: 'Alice Smith' }); // Push to a list (auto-generated key) const postsRef = ref(db, 'posts'); const newPostRef = push(postsRef); set(newPostRef, { title: 'Hello World', author: 'alice', timestamp: Date.now() });
Real-Time Listeners
Realtime Database's core feature is real-time synchronization. onValue listeners receive updates whenever the data at a path changes. The listener provides a snapshot of the current data. You can use snapshots to update your UI reactively. Listeners are efficient — they only receive the changed data. Realtime Database uses differential synchronization, sending only the changes. This minimizes bandwidth and latency. Always detach listeners when they're no longer needed to avoid memory leaks. Use once() for single reads without listening. Real-time listeners are the primary way to interact with Realtime Database. They enable the reactive patterns that make Firebase powerful.
import { getDatabase, ref, onValue, off } from 'firebase/database'; const db = getDatabase(); // Listen to data changes const userRef = ref(db, 'users/alice'); const unsubscribe = onValue(userRef, (snapshot) => { const data = snapshot.val(); console.log('User data:', data); }); // Listen to a list with child_added events const postsRef = ref(db, 'posts'); onChildAdded(postsRef, (snapshot) => { const post = snapshot.val(); console.log('New post:', post); }); // Detach listener // off(userRef);
Realtime Database vs Firestore
Realtime Database and Firestore serve different use cases. Realtime Database is optimized for low latency and high throughput with small, deeply nested data. Firestore is optimized for complex queries, filtering, and sorting with large, flat data structures. Realtime Database supports real-time synchronization natively with no additional latency. Firestore supports real-time listeners but with slightly higher latency. Realtime Database pricing is based on bandwidth and storage. Firestore pricing is based on reads, writes, and storage. Choose Realtime Database for chat, presence, and low-latency features. Choose Firestore for complex queries, data modeling, and scalability.
Best for: Chat apps, live dashboards, presence features, and low-latency sync. Data structure: Single JSON tree. Query capabilities: Limited (no filtering/sorting).
Best for: Complex queries, large datasets, relational-like data. Data structure: Collections and documents. Query capabilities: Rich (filtering, sorting, pagination).
Knowledge Check
Ready to test your understanding of 6. Realtime Database: Low-Latency JSON Storage?