EN DE
  • Home
  • Services
    • Dedicated team Your extended workbench at PITS
    • Web shops & websites Convincing websites for SMEs
    • Software development Complex projects made to measure
    • Hybrid or native iOS and Android apps Native iOS and Android apps
  • Initiatives
  • References
  • Technology
  • Process
  • About PITS
  • Contact
  • Media
    • Blog Our blog regularly provides you with current and exciting articles on a wide variety of topics from the online world.
    • White paper PITS Whitepapers are carefully prepared for developers as well as for customers on specific topics.
  • Jobs
  • Startups
�

Firebase – realtime database!

By Varun Bhuvanendran on February 6, 2017
Digital Coupons on Magento: 7 Metrics for 10X ROI
Digital Coupons on Magento: 7 Metrics for 10X ROI

By Dan Pinheiro

Starting with Hyperledger fabric

By Anizhasree K

NoSQL

By Thankaraj JJ

Augmented Reality- Trends & Tools

By Ijas Abubacker

Real-time applications need to process dynamic data that changes constantly over the time.  Implementing this functionality can a hectic stuff, which involves processing the incoming data and presenting it to the client side in real time.

Here we are looking at a cloud-based platform, Firebase,  which provides real-time sync along with many other services. There are real-time databases out there like rethink DB, and it’ s worth knowing the difference between the cloud-based sync services and traditional real-time databases.

So let’s try out a simple application using the firebase real-time database, which is only a tip of the iceberg, but might be good starting point for your real-time data adventures.

Create a firebase project to be used in our application. Go to the Firebase console, create and account, and start building the document using the simple to use user interface.

Next create an HTML file, which will be application file.

<!DOCTYPE html>
<html>
<head>
    <title>Sample web firebase project!</title>
</head>
<body>
    <ul id="list">
        <li>The contents will be updated in realtime!</li>
    </ul>

    <!--firebase scripts here-->

</body>
</html>

Next, we add the firebase library files to our application. The complete setup guide can be found here.
Then we need the firebase-app.js and firebase-database.js script files.

<script src="https://www.gstatic.com/firebasejs/3.6.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.2/firebase-database.js"></script>

 

We need to initialize firebase within our application using the public API values we get while creating the project in firebase console.

var config = {
    apiKey: "<API_KEY>",
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
};
firebase.initializeApp(config);

So we have initialized our firebase code, next we need to get a reference for our database.

var ref = firebase.database().ref();

With the ref variable, we perform various operations on our database. By default, the database we create through the firebase console requires authentication for all read-write operations. For testing purpose, we can turn that OFF. To do that in the firebase console dashboard, go to `RULES` tab and change the values like this

{ “rules”: {   “.read”: “true”,   “.write”: “auth != null” } }

This enables read operations without authentication and write operations protected. So now we are good to go with read operations. So we need to create some collections in the database and from our client side code, we could get and updated data every time the collections changes. A simple read implementation would look like this

 So now we are good to go with read operations. So we need to create some collections in the database and from our client side code, we could get and updated data every time the collections changes. A simple read implementation would look like this

ref.on("value", function (snapshot) {
// value from firebase db
var obj = snapshot.val();

// ul
var list = document.getElementById('list');

// remove elements in list if any
while(list.firstChild){
 list.removeChild(list.firstChild);
}

var content = '';

// append the data to the list
for( var key in obj.fruits ){
 var node = document.createElement('LI');
 content = document.createTextNode( obj.fruits[key].name +' - '+ obj.fruits[key].color );
 node.appendChild( content );
 list.appendChild( node );
}

}, function (error) {
 console.log("Error: " + error.code);
});

And that’s it, we have created a real-time sync application which could be extended in applications, like jekyll,  to serve some cool dynamic real-time contents.

Leave a Reply

You must be logged in to post a comment.

Copyright © 2021 PIT Solutions AG.An ISO 9001:2015 certified company. All Rights Reserved

Imprint

We'd love to hear from you.

Contact us

Switzerland
kontakt(at)pitsolutions(dot)ch
+41 43 558 43 60

India
contact(at)pitsolutions(dot)com
+91 471 270 0615 / 715

USA
pitsusa(at)pitsolutions(dot)com
+1 425 440 2812

UAE
pitsuae(at)pitsolutions(dot)com
+971 6 558 5598

Copyright © 2021 PIT Solutions AG.An ISO 9001:2015 certified company. All Rights Reserved

Imprint
Contact us!
SCROLL TO TOP