Firebase is a leading backend-as-a-service platform which was developed by Google. It is known to be very easy to learn and use as it has extensive documentation and is popular for many mobile and web applications. Some of the key features include the Realtime Database, authenication features, Cloud Firestore, Google Analytics, and Cross-Platform SDKs.
Firebase Realtime Database is a cloud-hosted NoSQL database. It allows you to store and sync data between your users in real-time. Therefore, when you build cross-platform apps all clients/users share one Realtime Database instance and automatically receive updates with the newest data. The setup is very minimal, security rules are highly customizable and intuitive
The Realtime Database has supports many use cases and has hundreds of features, here are the three most used ones.
It synchronizes data across all clients/users in real time. When data changes, every connected user is updated within a few miliseconds as it also has conflict management.
Data is accessibile even if the user's device goes offline. When the connection is restored, the user will recieve any changes to get back to the current server state.
There are no fixed schemas so you can structure your data as seen fit. There are a many best practices you can follow, but this allows you to pass all sorts of data between the app and database.
This set up will be for Flask and Python, equivalent setups can be done for other languages and frameworks as the core functionality is the same.
First go to the Firebase Console Click on "Add Project" and set it up as per your needs
First install Firebase from the terminal:
pip install firebase-admin
Next initialize it within your app:
import firebase_admin
from firebase_admin import credentials, db
cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://your-database-name.firebaseio.com'
})
# You can put your credentials elsewhere as a JSON file for security
Firebase stores data in JSON format. As a developer, it is important to follow good practices as JSON can get messy very quickly.
The Firebase team themselves have made an easy guide for this: Structure Guide
Here is a brief overview on common use cases of using and manipulating data.
# Creating a new user
users_ref = db.reference('users')
new_user_ref = users_ref.push({
'name': 'First Last',
'email': 'user@example.com'
})# Reading data from users
all_users = db.reference('users').get()
for user_id, user_info in all_users.items():
print(user_id, user_info['name'])# Updating a user's name
user_update_ref = db.reference('users/{user_id}'.format(user_id='user1'))
user_update_ref.update({
'name': 'NewFirst NewLast'
})# Deleting a user
delete_ref = db.reference('users/{user_id}'.format(user_id='user2'))
delete_ref.delete()# Querying users by name
query = db.reference('users').order_by_child('name').equal_to('Test User').get()
for user_id, user_info in query.items():
print(user_id, user_info)- Google Analytics gives comprehensive insights about the usage of your application
- Performance Monitoring helps you gain insight into the performance of your application on various platforms
- Scalability is great as it scales automatically, even for large applications
- Cross-Platform SDKs makes it easier for a developer to integrate services into various applications
- High data usage and having a lot of clients can incur heavy costs
- It does not support complex querying like other SQL databases
- Configuring security rules can requires attention to detail and must be tested throughly