Offline Support in Firebase, a Truly Mobile Datastore?

Share this article

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.

Frequently Asked Questions about Offline Support in Firebase

How does Firebase’s offline capabilities compare to other mobile datastores?

Firebase’s offline capabilities are designed to be robust and reliable. Unlike other mobile datastores, Firebase allows you to build responsive apps that remain functional even when network connectivity is lost. This is achieved through Firebase’s synchronization feature, which automatically syncs data between your app and the Firebase database. When your app goes offline, Firebase continues to serve and store data locally. Once connectivity is restored, Firebase automatically syncs the local data with the server data.

What are the cost implications of using Firebase’s offline capabilities?

Firebase offers a flexible pricing model that caters to different usage needs. The cost of using Firebase’s offline capabilities is included in the overall Firebase pricing. It’s important to note that Firebase offers a free tier, which includes access to Firebase’s offline capabilities. However, for more extensive usage, you may need to consider one of Firebase’s paid plans.

How does Firebase’s offline capabilities work with Android?

Firebase’s offline capabilities are fully supported on Android. Firebase provides a set of APIs that allow you to manage and sync data between your Android app and the Firebase database. When your app goes offline, Firebase continues to serve and store data locally. Once connectivity is restored, Firebase automatically syncs the local data with the server data.

How can I enable offline capabilities in Firestore?

Enabling offline capabilities in Firestore is straightforward. Firestore provides a set of APIs that allow you to manage and sync data between your app and the Firestore database. When your app goes offline, Firestore continues to serve and store data locally. Once connectivity is restored, Firestore automatically syncs the local data with the server data.

What are the billing implications of using Firestore’s offline capabilities?

The cost of using Firestore’s offline capabilities is included in the overall Firestore pricing. Firestore offers a flexible pricing model that caters to different usage needs. It’s important to note that Firestore offers a free tier, which includes access to Firestore’s offline capabilities. However, for more extensive usage, you may need to consider one of Firestore’s paid plans.

Thomas GrecoThomas Greco
View Author

Thomas Greco is a web developer based out of New York City specializing in full-stack development with the M.E.A.N. stack technologies. Before web development, Thomas worked as a graphic designer, and he continues to utilize his background in design when building web applications. Have a question for Thomas ? You can reach him on Twitter.

chriswdatabasesdatastoresFireBase
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week