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 - App success made simple
Overview
Firebase provides the tools and infrastructure
you need to develop, grow, and earn money from your app. This package supports
web (browser), mobile-web, and server (Node.js) clients.
For more information, visit:
This SDK is intended for end-user client access from environments such as the
Web, mobile Web (e.g. React Native, Ionic), Node.js desktop (e.g. Electron), or
IoT devices running Node.js. If you are instead interested in using a Node.js
SDK which grants you admin access from a privileged environment (like a server),
you should use the
Firebase Admin Node.js SDK.
Get the code (browser)
Script include
Include Firebase in your web application via a <script>
tag:
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
<script>
var app = firebase.initializeApp({
apiKey: '<your-api-key>',
authDomain: '<your-auth-domain>',
databaseURL: '<your-database-url>',
storageBucket: '<your-storage-bucket>',
messagingSenderId: '<your-sender-id>'
});
// ...
</script>
Note: To get a filled in version of the above code snippet, go to the
Firebase console for your app and click on "Add
Firebase to your web app".
npm bundler (Browserify, Webpack, etc.)
The Firebase JavaScript npm package contains code that can be run in the browser
after combining the modules you use with a package bundler (e.g.,
Browserify, Webpack).
Install the Firebase npm module:
$ npm init
$ npm install --save firebase
In your code, you can access Firebase using:
var firebase = require('firebase');
var app = firebase.initializeApp({ ... });
If you are using ES6 imports or TypeScript:
import * as firebase from 'firebase';
var app = firebase.initializeApp({ ... });
Include only the features you need
The full Firebase JavaScript client includes support for Firebase Authentication, the
Firebase Realtime Database, Firebase Storage, and Firebase Cloud Messaging. Including
code via the above snippets will pull in all of these features.
You can reduce the amount of code your app uses by just including the features
you need. The individually installable services are:
firebase-app
- The core firebase
client (required).firebase-auth
- Firebase Authentication (optional).firebase-database
- The Firebase Realtime Database (optional).firebase-storage
- Firebase Storage (optional).firebase-messaging
- Firebase Cloud Messaging (optional).
From the CDN, include the individual services you use (include firebase-app
first):
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase-messaging.js"></script>
<script>
var app = firebase.initializeApp({ ... });
// ...
</script>
When using the firebase npm package, you can require()
just the services that
you use:
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
var app = firebase.initializeApp({ ... });
If you are using TypeScript with the npm package, you can import just the
services you use:
// This import loads the firebase namespace along with all its type information.
import * as firebase from 'firebase/app';
// These imports load individual services into the firebase namespace.
import 'firebase/auth';
import 'firebase/database';
The type information from the import statement will include all of the SDKs,
not just the ones you have required
, so you could get a runtime error if you
reference a non-required service.
Get the code (Node.js - server and command line)
NPM
While you can write entire Firebase applications without any backend code, many
developers want to write server applications or command-line utilities using the
Node.js JavaScript runtime.
You can use the same npm module to use Firebase in the Node.js runtime (on a
server or running from the command line):
$ npm init
$ npm install --save firebase
In your code, you can access Firebase using:
var firebase = require('firebase');
var app = firebase.initializeApp({ ... });
// ...
Firebase Storage is not included in the server side Firebase npm module.
Instead, you can use the
gcloud
Node.js client.
$ npm install --save gcloud
In your code, you can access your Storage bucket using:
var gcloud = require('gcloud')({ ... });
var gcs = gcloud.storage();
var bucket = gcs.bucket('<your-firebase-storage-bucket>');
...
Firebase Cloud Messaging is not included in the server side Firebase npm module.
Instead, you can use the
Firebase Cloud Messaging Rest API.
API definition
If you use the
Closure Compiler or
compatable IDE, you can find API definitions for all the Firebase JavaScript API
in the included /externs
directory in this package:
externs/
firebase-app-externs.js
firebase-auth-externs.js
firebase-database-externs.js
firebase-storage-externs.js
firebase-messaging-externs.js
Changelog
The Firebase changelog can be found at
firebase.google.com.