Developer Docs

JavaScript Web Guide

Offline Capabilities

Getting Started

Flybase works even if your app loses its network connection temporarily. We provide several tools for monitoring presence and synchronizing local state with server state, which we will introduce in this section.

Managing Presence

In real-time applications, it is often useful to detect when clients connect and disconnect. For example, we may want to mark a user as 'offline' when their client disconnects.

Flybase provides some simple functions that allow data to be written when a client disconnects from the Flybase servers. These updates will occur whether the client disconnects cleanly or not, so we can rely on them to clean up data even if a connection is dropped or a client crashes. All Flybase write operations, including setting, updating, and removing, can be performed upon a disconnection.

Here is a simple example of writing data upon disconnection by using the onDisconnect function:

	var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
	ref.onDisconnect( function(){
		var name = $('#nameInput').val() || "anonymous";
		var text = "has left the building";
		messagesRef.push( {name:name, text:text} );
Detecting Connection State

For many presence-related features, it is useful for a client to know when it is online or offline. Flybase provides a special asynchronous listener called Connected which is updated every time the client's connection state changes. Here is an example:

	var ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
	ref.Connected( function( status ){
		if( status === true ){
		} else {
			alert("not connected");

Connected() will return a boolean value which is not synchronized between clients because the values are dependent on the state of the client.

In other words, if one client reads Connected() as false, this is no guarantee that a separate client will also read false.

Now, we're ready for the next article on deploying your apps in Flybase.