Firebase / Realtime Database

Comparing Firestore vs Realtime Database

Firebase offers two cloud-based, client-accessible database options to cater for your app needs: Firestore and Realtime Database. This tutorial will help you understand the differ…

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Covers Firebase Realtime Database for managing and syncing data across devices.

1. Introduction

Tutorial's Goals

The main goal of this tutorial is to compare two of Firebase's powerful database options: Firestore and Realtime Database. We will explore the key differences, advantages, disadvantages, and ideal use cases for each.

Learning Outcomes

By the end of this tutorial, you will understand:
- The core concepts of Firestore and Realtime Database
- When to use Firestore or Realtime Database
- How to interact with both databases using code examples

Prerequisites

A basic understanding of Firebase, JavaScript and databases will be helpful for this tutorial.

2. Step-by-Step Guide

Firestore

Firestore is a NoSQL document database that lets you easily store, sync, and query data for your mobile and web apps at a global scale. It organizes data as collections of documents, and can even have nested sub-collections.

Key Features:

  • Document-oriented: Data is saved in documents, which are organized into collections.
  • Expressive querying: You can perform complex queries over your data.
  • Offline support: Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even when offline.
  • Scalability: Firestore scales automatically to handle your app's load.

Realtime Database

Realtime Database is Firebase's original database. It's a NoSQL database that stores data as JSON and synchronizes it in realtime to every connected client.

Key Features:

  • Realtime: Realtime Database updates in real time. You don't have to refresh your browser to get the latest updates.
  • Offline support: Like Firestore, it allows mobile and web apps to remain responsive even when offline.
  • Scalability: While it can scale, it requires sharding to scale beyond 100,000 concurrent connections.

3. Code Examples

Firestore

// Initialize Firestore
const db = firebase.firestore();

// Add a new document
db.collection("users").add({
    first: "Alan",
    last: "Turing",
    born: 1912
})
.then((docRef) => {
    console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
    console.error("Error adding document: ", error);
});

Realtime Database

// Get a reference to the database service
const database = firebase.database();

// Write new post
function writeNewPost(uid, username, picture, title, body) {
  const postData = {
    author: username,
    uid: uid,
    body: body,
    title: title,
    starCount: 0,
    authorPic: picture
  };

  const newPostKey = firebase.database().ref().child('posts').push().key;

  const updates = {};
  updates['/posts/' + newPostKey] = postData;
  updates['/user-posts/' + uid + '/' + newPostKey] = postData;

  return firebase.database().ref().update(updates);
}

4. Summary

In this tutorial, we compared Firestore and Realtime Database. Firestore is more scalable and provides more robust querying, but Realtime Database can be more straightforward for simple data needs.

Next steps for learning could include diving deeper into the Firebase platform and exploring other services it offers, like authentication and cloud functions.

5. Practice Exercises

  1. Exercise: Create a Firestore database and write a function to add a new document to a collection.
  2. Exercise: Create a Realtime Database and write a function to add a new child to a node.
  3. Exercise: For both databases, write a function to retrieve data and display it in your app.

Remember, practice is the key to mastering any skill. Continue experimenting with Firestore and Realtime Database to understand their potential. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

Unit Converter

Convert between different measurement units.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help