The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps with our Android, iOS, and JavaScript SDKs, all of your clients share one Realtime Database instance and automatically receive updates with the newest data.
Prerequisites
- Add and configure the Firebase JavaScript client SDK into your app.
Create a Database
Navigate to the Realtime Database section of the Firebase console. You'll be prompted to select an existing Firebase project. Follow the database creation workflow.
Select a starting mode for your Firebase Security Rules:
- Test mode
Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Understand Firebase Realtime Database Rules section.
To get started with the web, iOS, or Android SDK, select test mode.
- Locked mode
Denies all reads and writes from mobile and web clients. Your authenticated application servers can still access your database.
Choose a region for the database. Depending on your choice of region, the database namespace will be of the form
<databaseName>.firebaseio.com
or<databaseName>.<region>.firebasedatabase.app
. For more information, see select locations for your project.Click Done.
When you enable Realtime Database, it also enables the API in the Cloud API Manager.
Configure Realtime Database Rules
The Realtime Database provides a declarative rules language that allows you to define how your data should be structured, how it should be indexed, and when your data can be read from and written to.
Initialize the Realtime Database JavaScript SDK
You must specify your Realtime Database URL when initializing your JavaScript SDK.
You can find your Realtime Database URL in the Realtime Database section of the
Firebase console. It will have the form https://<databaseName>.firebaseio.com
(for us-central1
databases) or https://<databaseName><region>.firebasedatabase.app
(for databases in all other locations).
Initialize your SDK using the following code snippet:
// Set the configuration for your app
// TODO: Replace with your project's config object
var config = {
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
// For databases not in the us-central1 location, databaseURL will be of the
// form https://[databaseName].[region].firebasedatabase.app.
// For example, https://your-database-123.europe-west1.firebasedatabase.app
databaseURL: "https://databaseName
.firebaseio.com
",
storageBucket: "bucket.appspot.com"
};
firebase.initializeApp(config);
// Get a reference to the database service
var database = firebase.database();
You're ready to start using the Firebase Realtime Database!
Next Steps
Learn how to structure data for Realtime Database.
Prepare to launch your app:
- Set up budget alerts for your project in the Google Cloud Console.
- Monitor your Usage and billing dashboard in the Firebase console. You can also monitor your Realtime Database Usage dashboard.
- Review the Firebase launch checklist.