Firebase data is stored in a NoSQL database and only available as a cloud service. It is a typical JSON tree database similar to many other NoSQL databases like MongoDB, Redis, or Casandra. However, there is a specific feature which differentiates Firebase from others.
To understand the benefits of Firebase, imagine that you are developing a chatting app. As users add comments in a topic, the app will retrieve comments (in order) from a remote server where the chat database resides. When the user is inactive in the chat room, the app still needs to retrieve other users’ comments as they occur, as long as the user remains in that chat room. There are many challenges with building this type of periodic synchronization from scratch, and Firebase offers a solution. When you point your app to the proper data source in Firebase, it will retrieve the data in real-time. This will save a tremendous amount of custom development time.
The Firebase real-time database feature can be used for any of the following.
There are some other advantages to using Firebase:
The sample code below is written for Android, but iOS usage is very similar.
Add a dependency in your build.gradle file. This is all that is needed to use Firebase.
compile 'com.google.firebase:firebase-database:9.6.1'
Next, you can initialize Firebase in your code and point it to the right database.
FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("messages");
Building upon the chatting app, you can structure the message database as below. Let’s simplify things by assuming that the message node contains all the message from the users. This will continuously grow as users add more comments.
"messages": { {"message":"This is a test message"},{"message":"How are you?"} }
The following method will be called once with the initial value and whenever data at this location is updated.
// Read from the database. Called whenever it's updated. myRef.child("message").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String value = dataSnapshot.getValue(String.class); Log.d(TAG, "Value is: " + value); } @Override public void onCancelled(DatabaseError error) { // Failed to read value Log.w(TAG, "Failed to read value.", error.toException()); } });
The write function is simple as shown below.
myRef.child("message").setValue("Hello, World!");
There are more features in Firebase that can be leveraged in your app development. For more information, check out the documentation from the following reference: https://firebase.google.com/docs/
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.