Real-time data sync without managing a server.
Firebase is Google's app-development platform, and Firestore is its cloud database. Unlike a traditional database that you install, host, and write a backend for, Firestore is a managed service your frontend talks to directly. Its standout feature is real-time sync: when data changes, every connected client sees the update instantly — no WebSocket code, no polling, no server to run.
Firestore is a document database: instead of tables and rows, data lives in JSON-like documents grouped into collections.
Firebase shines for chat apps, live feeds, collaborative tools, and multiplayer features — anything where users should see each other's changes immediately. The free tier comfortably covers learning projects and small apps. The trade-offs: complex queries are harder than in SQL, costs can grow with heavy usage, and you're tied to Google's ecosystem. For structured data without real-time needs, PostgreSQL or MySQL gives you more querying power and portability.
messages/abc123.
onSnapshot() and your callback
re-runs whenever the data changes, on any device.
import { collection, addDoc, onSnapshot } from "firebase/firestore";
// Add a message
await addDoc(collection(db, "messages"), {
text: "Hello!",
sentAt: Date.now(),
});
// Live-update the UI whenever any message changes
onSnapshot(collection(db, "messages"), (snapshot) => {
const messages = snapshot.docs.map((d) => d.data());
render(messages); // runs on every change, on every client
});
That's a working real-time chat backend. Open the app in two browser tabs and messages appear in both instantly.
npm install firebase and initialize it with that
config.
addDoc().
onSnapshot() — watch two tabs update each other.