What is firebase?
The firebase npm package is a comprehensive app development platform provided by Google that offers a variety of services such as real-time databases, authentication, cloud storage, hosting, and more. It is designed to help developers build and manage apps more efficiently.
What are firebase's main functionalities?
Realtime Database
Firebase Realtime Database allows you to store and sync data between your users in real-time. This is a NoSQL database that lets you build rich, collaborative applications by allowing secure access to the database directly from client-side code.
const { initializeApp } = require('firebase/app');
const { getDatabase, ref, set } = require('firebase/database');
// Initialize Firebase
const app = initializeApp({ /* your config */ });
const database = getDatabase(app);
// Write data to your database
set(ref(database, 'users/1'), {
username: 'example',
email: 'user@example.com'
});
Authentication
Firebase Authentication provides backend services to help authenticate users, including simple sign-in functionality as well as third-party providers like Google, Facebook, and Twitter.
const { initializeApp } = require('firebase/app');
const { getAuth, createUserWithEmailAndPassword } = require('firebase/auth');
// Initialize Firebase
const app = initializeApp({ /* your config */ });
const auth = getAuth(app);
// Create a new user
createUserWithEmailAndPassword(auth, 'user@example.com', 'password')
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
// Error handling
const errorCode = error.code;
const errorMessage = error.message;
// ...
});
Cloud Firestore
Cloud Firestore is a flexible, scalable database for mobile, web, and server development. It keeps your data in sync across client apps through real-time listeners and offers offline support.
const { initializeApp } = require('firebase/app');
const { getFirestore, collection, addDoc } = require('firebase/firestore');
// Initialize Firebase
const app = initializeApp({ /* your config */ });
const db = getFirestore(app);
// Add a new document with a generated id
addDoc(collection(db, 'users'), {
first: 'Ada',
last: 'Lovelace',
born: 1815
});
Cloud Storage
Firebase Cloud Storage is built for app developers who need to store and serve user-generated content, such as photos or videos.
const { initializeApp } = require('firebase/app');
const { getStorage, ref, uploadBytes } = require('firebase/storage');
// Initialize Firebase
const app = initializeApp({ /* your config */ });
const storage = getStorage(app);
// Create a storage reference from our storage service
const storageRef = ref(storage, 'some-child');
// Upload file
uploadBytes(storageRef, file).then((snapshot) => {
console.log('Uploaded a blob or file!');
});
Hosting
Firebase Hosting provides fast and secure hosting for your web app, static and dynamic content, and microservices.
const { initializeApp } = require('firebase/app');
const { getAuth } = require('firebase/auth');
const { getFirestore } = require('firebase/firestore');
const { getStorage } = require('firebase/storage');
// Initialize Firebase
// TODO: Replace the following with your app's Firebase project configuration
const firebaseConfig = {
//...
};
const app = initializeApp(firebaseConfig);
// The rest of your web app's Firebase initialization and setup goes here...
// For example, you might set up Firebase Authentication, Firestore, and Storage as shown above.
Other packages similar to firebase
parse-server
Parse Server is an open-source version of the Parse backend that can be deployed to any infrastructure that can run Node.js. It offers many of the same features as Firebase, such as a real-time database, file storage, and user authentication. However, being open-source, it provides more flexibility and control over the backend infrastructure.
aws-amplify
AWS Amplify is a development platform for building secure, scalable mobile and web applications. It provides a similar range of services as Firebase, including authentication, data storage, and hosting. Amplify is tightly integrated with AWS services, which can be a benefit if you are already using AWS in your stack.
realm
Realm is a mobile database and synchronization platform that can be used for building offline-first, reactive mobile experiences. It offers real-time synchronization and data storage capabilities similar to Firebase's Realtime Database and Firestore. Realm is known for its smooth integration with mobile development and its fast performance on mobile devices.
Firebase Javascript SDK
The Firebase JavaScript SDK implements the client-side libraries used by
applications using Firebase services. This SDK is distributed via:
To get starting using Firebase, see
Add Firebase to your JavaScript Project.
SDK Dev Workflow
Prerequisites
Before you can start working on the Firebase JS SDK, you need to have Node.js 4.0 or
greater installed on your machine. After doing this, you must also install the
dependencies for this package.
To download Node.js visit https://nodejs.org/en/download/.
Once you've verified that you are using version 4.0 or later (run node -v
to see your
current running version of Node.js), you can install the dependencies by running:
$ npm install
NOTE: This package also maintains a yarn.lock
so you can get faster installs by installing
dependencies with yarn
instead.
Pipeline Instructions
The Firebase JS SDK is built and tested through a gulp pipeline. You will need to
have the gulp
command available on your system to run the tasks yourself.
To install gulp
simply run:
$ npm install -g gulp-cli
NOTE: Installing gulp-cli
is optional as you can simply leverage the npm commands
for most interactions.
Gulp Pipeline
Most of the tasks for interacting with the SDK are defined through gulp. If you
installed gulp globally, you can run the following to see all of the available
gulp tasks:
gulp --tasks
Testing the SDK
To run all tests for the SDK simply run: npm test
at the root of this package.
There are several types of available tests:
- Unit Tests (
gulp test:unit
) - Smoke Tests (
gulp test:smoke
) - Integration Tests (
gulp test:integration
)
NOTE: You can execute each of these tasks on their own (e.g. you can run
gulp test:unit
to run exclusively the smoke tests from your CLI)
Integration Tests
The integration tests are designed to run against a live Firebase instance. Because
of this a little pre-config is required.
- Create a
project.json
This file should be placed in tests/integration/config
and should contain your
Firebase project's config options.
Visit https://console.firebase.google.com/, select the project you wish to test
with, and click the "Add Firebase to your web app" affordance to get this config.
i.e.
{
"apiKey": "---",
"authDomain": "---",
"databaseURL": "---",
"projectId": "---",
"storageBucket": "---",
"messagingSenderId": "---"
}
- Create a
key.json
This file should be placed in tests/integration/config
. This script should be a valid service
account for your firebase project.
You can follow the instructions here
to create a valid service account for your project.
Building the SDK
Introduction
The Javascript SDK is built through a gulp pipeline.
To build the project run npm run build
in your CLI.
This will generate all of the output assets in a /dist
folder available at the
root of this project.
Each of the different types of source files are explained more in detail below.
Source File Handling
Our source files are all located in the /src
directory. This currently contains
a variety of different sources (typescript, prebuilt binaries, legacy). We handle
each of these cases in our gulp pipeline.
Typescript Source
This is the planned source language for this repo. As we are able, all components
will be migrated to typescript and processed in the following flow:
- TS Files are compiled to ES6 using the Typescript compiler
- ES6 Files are transpiled to CJS Modules using Babel
- CJS Modules are attached to a window global using Webpack
Prebuilt Binaries
To allow firebase to build from Github we consume prebuilt binaries of our components
until the source is migrated to this repo.
These files are processed in the following flow:
- Prebuilt browser binaries are ready to consume individually in the browser
however we need to wrap them in a CJS module wrapper for node/webpack/browserify
consumption.
- The Firebase App binary is generated (from TS) and concatenated with the
browser binaries of each individual module to create
firebase.js
.
Legacy Files
These files are here to simply allow for backwards compatibility and are simply
moved into their required locations
Contributing
See Contributing for more information on contributing to the Firebase
JavaScript SDK.