Using Firebase for real-time apps

Looking for a new way to develop an app? Web Developer Armin Solecki discusses how he uses Firebase for application development and the benefits on offer.

Firebase offers a different approach for building applications. It is a fully featured service that can power applications backend, store data, host static files and maintain user authentication.

These features give a solid foundation into app development and we can focus on offering the best user experience, instead of choosing and fine tuning the development stack which involves technology setup, environment settings and deployment techniques. Furthermore, these out of the box features save a lot development time and create a lot of value to the user.

Real-time database

Firebase offers an innovative way of storing data. It’s a non-relational database that is stored in the JSON format. The JSON data format is widely used which makes it very easy to implement in any technology stack. The database also comes with a software development kit (SDK) that speeds up data integration and implementation. Currently Android, iOS and JavaScript SDKs are provided by Firebase which covers all the main and most popular platforms with highest user base. In addition to this, the Firebase team are maintaining data binding libraries for the most widely used JavaScript frameworks that most front end developers use. Those include data bindings for Backbone, AngularJS, Ember and ReactJS. For those who cannot move into new way of app building, there is REST API that can be used with the existing backend.

The best feature of Firebase is that the database is real-time, which means that any changes or updates to the database are instantly synchronised with all clients and users who are using the app. This makes development real time apps like traffic updates, games or live chat a fast and easy. The developer needs just to focus on binding the data into presentation layer and the updates are done automatically when anything changes in the database. The database scales really well and can serve many real-time users without any problem.

With all the libraries provided, building applications is fast and only requires few lines of code. In order to connect and manipulate the database you just require following code:

// Create a connection to your Firebase database
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
// Save data
ref.set({ name: "John" });
// Listen for realtime changes
ref.on("value", function(data) {
var name = data.val().name;
alert("My name is " + name);
});

Authentication

Firebase service offers a range of authentication services that are easy to implement. It has built-in support for all the major OAuth authentication services like Google, Twitter, Facebook or Github. Furthermore, you can setup your own email and password authentication or custom auth tokens authentication. All the options provided by Firebase save a lot of time and effort that you’d need to put into the solution when you are implementing some of those third party services. Here’s a sample piece of code to authenticate a user via Twitter:

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithOAuthPopup("twitter", function(error, authData) {
if (error) {
console.log("Authentication Failed!", error);
} else {
console.log("Authenticated successfully with payload:", authData);
}
});

Other features

Firebase offers static file hosting which is perfect for single page apps that do not require any backend. Each app is secured via SSL certificate by default which makes all the apps more secure. The database works offline, which is a nice feature to have when users have connectivity or latency problems. The data is stored locally and all the changes are synchronized with the server when connection is re-established. The Firebase team also implemented security rules that can be used for some custom read/write access or data input validation. The security rules are JSON-based, which makes learning how they work much easier and intuitive.

Summary

Firebase is a completely new way of building applications and it’s worth giving it a try. It’s really a very exciting way of building applications because it gives front end developers a way of storing data without any backend/database environment setup. The API is very promising and only requires few lines of code to work. There’s also extensive documentation that features all the necessary information to start developing in Firebase and features sample applications built using Firebase. Firebase offers a free plan that includes all the features so why not give it a try?

By Armin Solecki, Web Developer