Firebase (Firestore)

Real-time data sync without managing a server.

What is it?

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.


When to use it

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.


Core concepts
  • Documents and collections — a document is a set of key-value fields (like a JSON object); collections hold documents: messages/abc123.
  • Real-time listeners — instead of fetching once, you subscribe with onSnapshot() and your callback re-runs whenever the data changes, on any device.
  • Direct-from-client access — your web app reads and writes the database directly with the Firebase SDK; there's no backend API layer to build.
  • Security rules — because clients talk to the database directly, access control lives in Firestore's rules language ("users may only edit their own documents"). Getting these right is essential.
  • Authentication built in — Firebase Auth gives you Google/email/password sign-in in a few lines, and integrates with security rules.

A taste of the code
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.


Your first steps
  1. Create a free project in the Firebase console and add a web app to get your config snippet.
  2. In a frontend project (plain JS or React), install the SDK with npm install firebase and initialize it with that config.
  3. Enable Firestore in test mode and write your first document from code with addDoc().
  4. Build a tiny live guestbook or chat using onSnapshot() — watch two tabs update each other.
  5. Add Firebase Auth, then lock down your security rules before sharing the app with anyone.

← Back to Stack Picker All primers