23
Aug

Firebase Realtime Database – Installation and Setup

Firebase Application

Realtimedatabase : FIREBASE

It is a NoSQL database for building Web, Android, and iOS applications. It stores data as JSON objects, it does not have tables or records like a SQL database. It is a real-time database and offers different APIs for different operations. Data is stored as JSON, allowing nesting data up to 32 levels deep. It is synchronized in real-time to every connected client like mobile, desktop window/iOS app, web app etc., everywhere. This database best suits chat applications and websites where data changes frequently. All connected clients automatically receive updates with the newest data.

{ “users”: { “alovelace”: { “name”: “Ada Lovelace”, “contacts”: { “ghopper”: true }, }, “ghopper”: { … }, “eclarke”: { … } } }

Installation and Setup for Web Application:

First of all, create a Firebase project in the google firebase console. Then click Add App from the project overview page; click Add Firebase to your web app. You’ll get a code snippet, copy that and put it in your project with your configuration like apiKey, auth domain, database URL etc., which you’ll get from your newly created app.

You may need many components in your app, and you can include them in your website according to your requirement. The final code will be:

<scriptsrc="https://www.gstatic.com/firebasejs/4.1.2/firebase-app.js"></script>
 <scriptsrc="https://www.gstatic.com/firebasejs/4.1.2/firebase-auth.js"></script>
 <scriptsrc="https://www.gstatic.com/firebasejs/4.1.2/firebase-database.js"></script>
 <scriptsrc="https://www.gstatic.com/firebasejs/4.1.2/firebase-messaging.js"></script>

// Set the configuration for your app
 // TODO: Replace with your project's config object
 varconfig={
 apiKey:"apiKey",
 authDomain:"projectId.firebaseapp.com",
 databaseURL:"https://databaseName.firebaseio.com",
 storageBucket:"bucket.appspot.com"
 };
 firebase.initializeApp(config);

// Get a reference to the database service
 var database =firebase.database();

That’s it, and you are ready to use this database.

Save Data to Firebase:

Suppose you’ve created a collection named “students” in the “samplefirebase” database,

varstudentsRef=firebase.database().ref("students/"); 
studentsRef.set({
John:{
number:1,
age:25
},  
Jucy:{
number:2,
age:20
}
});

You’ll this data will be added in firebase console.

Update Record:

varjohnRef=firebase.database().ref("students/John");
 johnRef.update({
"number":40
});

That’s it, Simple ?

Pushing data with unique-ID:

It is the same as saving/updating data; the structure will change in the database, and now the id will be auto-generated and unique instead of student names.

varref=newFirebase('https://samplefirebase.firebaseio.com'); 
varstudentsRef=ref.child("students");
studentsRef.push({
name:"John",
number:1,
age:25
});

studentsRef.push({
name:"Jucy",
number:2,
age:20
});

Transactional Data:

It is used when we want to do some calculation on any data and save back the updated data, for example:

varref=newFirebase('https://samplefirebase.firebaseio.com'); 
varjucyAgeRef=ref.child("students").child("-YTr8-KJghmjg87mh").child('age');
 jucyAgeRef.transaction(function(currentAge){
returncurrentAge+1;
});

Reading and writing data:

varref=firebase.database().ref('students/-YTr8-KJghmjg87mh/age);
ref.on('value',function(snapshot){
console.log(snapshot.val());
});

Here, the “on” method is to read data and listen to changes, which take “value” events and response as snapshot, snapshot.val() function returns data in javascript representation.

Read data once:

Sometimes you may want a snapshot of your data without listening for changes for the element you don’t expect to change. In such case, use once() instead of on()

varuserId=firebase.auth().currentUser.uid;
returnfirebase.database().ref('/users/'+userId).once('value').then(function(snapshot){
var username =snapshot.val().username;
// ...
});

Deleting Data:

This can be done by remove() method or set(null)

That’s all about basic programming.

Benefits of using Firebase:

  • Real-time database: There is no need to make your abase or your own API. Firebase handles everything itself and updates information on all connected.
  • Hosting: In Firebase, sending data to CDN with a single command is easy, so there is no issue in the host db on any server for every update.
  • Authentication: Firebase has UI to authenticate users by email-id, password or username.
  • Storage: Firebase has the facility to store pictures, sound, video etc, on Google Cloud storage.
  • Cloud messaging: Cross-platform messaging, sending notifications to users is easy and inexpensive with Firebase.
  • Test Lab: You must avoid writing your own test codes; the Google data centre hosts many gadgets and provides your application’s test logs, videos, and screenshots.
  • Crash Reporting: It is very easy in Firebase to register custom events to help capture the steps which lead to a crash.
  • Notifications: It is a free service of Firebase to notify mobile users of any updates.

So, firebase is one of the best solutions for all your needs related to cross-platform applications.

Ficode Technologies is a leading mobile app development company . Our experienced team is well-versed in building mobile applications for all the major platforms, whether it’s iOS, Android & Windows. To learn how an awesome app can swiftly take your business to the next level, contact us immediately!

share

MySQL Joins - Understand Inner and Outer Joins Query

MySQL Joins - Understand Inner and Outer Joins Query

previous-blog-arrowPrevious
Difference Between Cakephp 2 and Cakephp 3

Difference Between Cakephp 2 and Cakephp 3

next-blog-arrowNext