Firebase is a BaaS platform which gives you set of services that help you start and scale your apps. It shares the same underlying infrastructure as Google Cloud and is placed on the same bill.
Firebase consists of the following array of products:
- Firebase Auth for setting up end-to-end user authentication without writing and maintaining the backend for it yourself.
- Firebase DB, a realtime database (described as a giant JSON tree that bidirectionally communicates with clients, meaning updates can be pushed to connected clients).
- Cloud Firestore, a NoSQL database.
- Firebase Hosting for deploying web apps and static content to a CDN.
- Cloud Storage for Firebase for storing files/blobs.
- Cloud Functions for Firebase for running backend code in response to events without having to maintain your own servers or cloud VMs. Itâs essentially a thin wrapper around Google Cloud Functions (see StackOverflow).
- Firebase Remote Config. ⊠and a few more. They all have âclient-firstâ SDKs for JavaScript, Android, iOS, Flutter, Unity, etc. which means you can directly interact with Firebase products from your frontend without a backend.
The main concern of adopting Firebase is vendor lock-in. Donât worry too much about it though.
Web Quick Setup
Firebase Auth
Firebase Auth provides a bunch of services for implementing user registration, sign in, and sign out for your app in just a few lines of code. It also becomes painless to set up multiple auth providers like Google, Facebook, GitHub, etc.
Snippets
Some code snippets to demonstrate Google sign-in, sign-out and grabbing Google profile information (sourced from the official Codelab):
Firebase Realtime Database
Firebase DB is a NoSQL realtime database, credited with low latencies. Being a ârealtimeâ database just means that any updates from one client can be pushed to subscribed clients within milliseconds. In other words, itâs like a pubsub system for quickly syncing and notifying clients with small bits of data. Firebase DB is a great choice compared to ânormalâ databases like Cloud Firestore or PostgreSQL if you have a simple data model, small amount of data, and expect low-latency access. For more heavyweight use cases and longer term data storage, pick Cloud Firestore instead.
All data is stored as JSON, in fact a Firebase DB instance is described as just âa cloud-hosted JSON treeâ. It looks like this, for example:
When users lose network connection, the changes theyâd otherwise push to the database are persisted locally in a cache, and then when they reconnect, those changes are automatically merged with the database.
Usage
Initialise the Firebase SDK, create a Firebase database instance through the web console, then specify the databaseURL
field in the initialisation config object.
Local Realtime DB Emulator
Since you likely donât want to read/write data to a production database server while youâre developing, you should use the local emulator for the realtime db, provided by Firebase. Youâd also want to use this to integration or e2e tests.
Then in your client-side code, connect to it if youâre locally hosting your website:
Security Rules
Security rules let you set the conditions that have to be passed to allow read or write access to a certain node in the database. You can also set data validation rules that enforce simple checks such as making sure that a field is a string with a certain length. Theyâre specified in a file called database.rules.json
by default. Applying the rules in database.rules.json
is done with firebase deploy --only database
.
Example database.rules.json
:
Firebase CLI
The Firebase CLI is for deploying and managing projects from the terminal.
Some useful commands: