DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Firebase: Real-Time Database Using AngularJS

Firebase: Real-Time Database Using AngularJS

Firebase provides a powerful real-time database that can be accessed from almost anywhere. You only need to reference a couple libraries to add it to your web app!

Ezequiel Reyno user avatar by
Ezequiel Reyno
·
Feb. 22, 17 · Tutorial
Like (13)
Save
Tweet
Share
18.09K Views

Join the DZone community and get the full member experience.

Join For Free

Today, we're going to talk about Firebase, a powerful Google mobile platform that provides a bunch of tools such as storage, cloud messaging, hosting, and a real-time database that can be accessed via web, iOS, or Android app.

Adding Firebase to Your Web App

I'm going to explain how to insert a JSON object into a real-time database and restore that value using AngularJS. You only need to reference AngularJS, Firebase, and AngularFire libraries in your project.

<!-- AngularJS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>

<!-- Firebase -->
<script src="https://www.gstatic.com/firebasejs/3.6.4/firebase.js"></script>

<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/2.2.0/angularfire.min.js"></script>

Then, we need to add Firebase configuration in our index.html. To do that, we need to go into the Firebase console and get the initialization code, as you can see in the image below.

initcode

Creating Your Data Layer

I will create an AngularJS service to use as a data layer. This service will be responsible for creating our links to the Firebase real-time database. Here, we are going to use two important functions in the firebase library. The first one is ref() from the database() object. This function returns a direct link to the root structure of our documental database. The second function is child(), which allows us to create child reference to this root structure.

 function firebaseDataService() {
     var root = firebase.database().ref();

     var service = {
         root: root,
         requests: root.child('requests')
     };

     return service;
 }

In the example above, we have a path to the root structure and a child of this path call requests .

Using AngularFire to Manipulate the Reference

Now, we have a service to get a link to our database reference, so we are going to use Angular Fire to create an object with access to our database.

function getRequestsByUser(uid) {
    if (!requests) {
        requests = $firebaseArray(firebaseDataService.requests.child(uid).child('userRequests'));
    }
    return requests;
}

In the example above we use $firebaseArray function to create an object, the parameter this function uses is a path reference to Firebase. This object is now linked to our database which means that every time you put or remove an object from this array is going to affect our database in real time. We can use the function $add or $remove to manipulate our database. Our database looks like this:

db

That's it. After doing that, we are ready to go. I have a working example in this link if you want to see the complete implementation. In this example, I created an app that gives you the current time in different places around the world.

For a complete reference of Angular Fire use this link.

For a complete reference of Firebase use this link.

If you found this post useful, please don't forget to press the like button and share it. If you are in doubt, don't hesitate to ask a question. As always, thank you for reading.

Database Firebase AngularJS

Published at DZone with permission of Ezequiel Reyno. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Software Maintenance Models
  • LazyPredict: A Utilitarian Python Library to Shortlist the Best ML Models for a Given Use Case
  • Metrics Part 2: The DORA Keys
  • All the Cloud’s a Stage and All the WebAssembly Modules Merely Actors

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: