Skip to content

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:

const ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
ref.onDisconnect( function(){
let name = $('#nameInput').val() || "anonymous";
let 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 Connectedwhich is updated every time the client’s connection state changes. Here is an example:

const ref = new Flybase("74k8064f-cd6f-4c07-8baf-b1d241496eec", "web", "posts");
ref.Connected( function( status ){
if( status === true ){
alert("connected");
} 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.