I have two related questions regarding the Firebase web platform's synchronisation of locally-modified data to the server:
Every client sharing a Firebase database maintains its own internal version of any active data. When data is updated or saved, it is written to this local version of the database. The Firebase client then synchronizes that data with the Firebase servers and with other clients on a 'best-effort' basis.
1. Handling sync errors
The data-modification methods
(set()
,
remove()
, etc)
can take an onComplete
callback parameter:
A callback function that will be called when synchronization to the Firebase servers has completed. The callback will be passed an
Error
object on failure; elsenull
.var onComplete = function(error) { if (error) { console.log('Synchronization failed'); } else { console.log('Synchronization succeeded'); } }; fredRef.remove(onComplete);
In the example above, what kind of errors should the fredRef.remove()
callback expect to receive?
- Temporary errors?
- Client is offline (network connection lost) ?
- Firebase server is temporarily overloaded or down for maintenance, but will be available again soon?
- Permanent errors?
- Permission denied (due to security rules) ?
- Database location does not exist?
Is there a way to distinguish between temporary and permanent errors?
How should we handle / recover from these errors?
For temporary errors, do we need to call fredRef.remove()
again after a short period of time, to retry the operation?
2. Global sync status
I realise that each call to set()
and remove()
will receive an individual sync success/failure
result in the onComplete
callback. But I'm looking for a way to determine the
global sync status of the whole Firebase client.
I'd like to use a beforeunload
event listener
to warn the user when they attempt to leave the page before all modified data has been synced to the server,
and I'm looking for some function like firebase.isAllModifiedDataSynced()
. Something like this:
window.addEventListener('beforeunload', function (event) {
if (!firebase.isAllModifiedDataSynced()) {
event.returnValue = 'Some changes have not yet been saved. If you ' +
'leave this page, your changes will be lost.';
}
});
Here's an example of the same functionality in Google Drive:
I'm aware of the special /.info/connected
location:
it is useful for a client to know when it is online or offline. Firebase clients provide a special location at
/.info/connected
which is updated every time the client's connection state changes. Here is an example: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"); } });
The special /.info/connected
location can be connected to a beforeunload
event listener like this:
var connectedRef = new Firebase('https://myapp.firebaseio.com/.info/connected');
var isConnected = true;
connectedRef.on('value', function (snap) {
isConnected = snap.val();
});
window.addEventListener('beforeunload', function (event) {
if (!isConnected) {
event.returnValue = 'Some changes have not yet been saved. If you ' +
'leave this page, your changes will be lost.';
}
});
My question is:
- If
isConnected
istrue
, does this also mean that all modified data has been synced to the server? - i.e. Does "connected" also mean "synced"?
If not, how can the app determine the global sync status of the whole Firebase client?
- Is there a special
/.info/synchronized
location? - Does the app need to manually keep track of the sync success/failure result of every
onComplete
callback?