Offline Support in Firebase, a Truly Mobile Datastore?
Since being acquired by Google, Firebase has continued to develop into a powerful platform for building both web and mobile applications.
The company’s recent announcements from the Firebase team dealt with their pricing structure, which was changed dramatically. In the past some may have found Firebase too expensive, it is now significantly cheaper. As a result of this decrease in cost, there are sure to be a bunch of cool projects that utilize Firebase’s real time capabilities. More interesting to developers, Firebase’s development team announced full offline support for the platform at the annual Google I/O Conference in May. In this tutorial I intend to find out just how useful and usable the new features are.
Mobile-minded
The Firebase team’s intentions regarding mobile development have been clear from the start. They understand how important a user’s mobile experience is, and continously bear it in mind when crafting their technology. With offline support, developers at Firebase understand that devices lose their network signal once, if not multiple times throughout the day.
The team believes that the data from an application should persist even when disconnected. They believe that just because you may have lost service, you shouldn’t lose your data. Keeping this in mind, they developed Firebase so that application data persists through situations where service is interrupted.
Offline Data Synchronization via Local Firebase Instance
Firebase uses synchronization when sending data from the client to the server. This differs from the traditional method of using a system of requests and responses. As a result, the Firebase SDK can save any of your application data against a local version of your database. To perform this, Firebase uses a system of local read operations that allows data to persist locally until a connection is re-established. Once a connection is re-established, Firebase will fire off the necessary functions and catch up with the server.
To trigger this offline support for Firebase’s Android and iOS SDKs, add the following code to your app.
iOS
[Firebase defaultConfig].persistenceEnabled = YES;
Android
Firebase.getDefaultConfig().setPersistenceEnabled(true);
By defining this in your application, you are telling Firebase to persist all of your data.
Firebase included the new keySynced
feature with the release of its offline support. When set to true, keySynced
can prefetch specific data from your application.
The following examples enable this feature.
iOS
[ref keepSynced:YES];
Android
ref.keepSynced(true);
When added to your application, Firebase allows developers to specify the data that they want users to have access to this feature.
Offline JavaScript Capabilities
Firebase also added functionality to the JavaScript API. It’s important to note that offline support for the JavaScript API is meant primarily to manage the presence of a user. Firebase provides a location for JavaScript developers located at .info/connected
used explicitly to detect the presence of a user. When used, this location will return a boolean value and tell developers whether a client is connected or not. Below is an example of how this /.info/connected
folder can be used.
var connectedRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/connected");
connectedRef.on("value", function(snap) {
if (snap.val() === true) {
alert("connected")
} else {
alert("not connected");
}
});
From this, developers can log information about a connection depending on these values. Firebase provides the onDisconnect
class, which allows developers to define specific actions that should happen when a connection is lost. The onDisconnect
class can be using with the following methods:
-
set()
-
remove()
-
update()
When paired with onDisconnect
, these methods allow us to write or clear different data from an application when a user loses connection. Below, I have placed some code that shows usage of the onDisconnect
method to log the time of which a connection was lost. As a result, a timestamp will be sent to the /lastConnected
directory as soon as the client disconnects from Firebase.
var lastConnectedRef = new Firebase("https://.firebaseio.com/users/joe/lastConnected");
lastConnectedRef.onDisconnect().set(Firebase.ServerValue.TIMESTAMP);
Conclusion
This change in pricing structure combined with offline support makes Firebase a more viable option when choosing a solution for handling your application’s data. Firebase is currently implemented in the projects of over one-hundred and ninety developers and hopefully you will now try the service yourselves.
Feel free to comment below if you have any questions, and I will get back to you as soon as possible.