Firebase synchronization of locally modified data: error handling and global status

I have two related questions regarding the Firebase web platform . synchronization of locally modified data on the server :

Each 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 this data with Firebase servers and other clients based on "best effort."


1. Handling synchronization errors

Data modification methods ( set() , remove() , etc.) can be taken by the onComplete callback parameter:

A callback function that will be called when synchronized with Firebase servers is complete. The callback will be passed to the Error object on error; else null .

 var onComplete = function(error) { if (error) { console.log('Synchronization failed'); } else { console.log('Synchronization succeeded'); } }; fredRef.remove(onComplete); 

In the example above, what errors should be expected from the fredRef.remove() callback?

  • Temporary errors?
    • Client disconnected (loss of network connection)
    • Firebase server is temporarily overloaded or shut down for maintenance, but will be available again soon
  • Permanent errors?
    • Permission denied (due to security rules )?
    • Does the database location not exist?

Is there a way to distinguish between temporary and permanent errors?

How should we handle / repair these errors?

For temporary errors, do we need to call fredRef.remove() again after a short time to repeat the operation?


2. Global sync status

I understand that each set() and remove() call will receive an individual success / failure synchronization will result in an onComplete callback. But I'm looking for a way to determine the global synchronization status for the entire Firebase client .

I would like to use the beforeunload event listener to alert the user when they try to leave the page before all the changed data is synchronized with the server, and I'm looking for some function like firebase.isAllModifiedDataSynced() . Something like that:

 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 is an example of the same functionality in Google Drive:

Google drive screenshot

I know a special /.info/connected location :

It is useful for the client to know when they are online or offline. Firebase clients provide a special location in /.info/connected , which is updated each time the client's connection status 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 location /.info/connected can be connected to the beforeunload event beforeunload as follows:

 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 true , does this mean that all changed data has been synchronized with the server?
  • i.e. Is connected " means " synchronized " ?

If not, how can an application determine the global synchronization status of an entire Firebase client?

  • Is there a special location /.info/synchronized ?
  • Does the application need to manually track the result of successful / unsuccessful synchronization of each onComplete callback?
+5
source share
1 answer

In the example above, what errors should be expected from the fredRef.remove () callback?

Is the client disconnected (loss of network connection)?

No, this will not cause the error to be passed to the completion listener. This will simply cause the completion listener to not be called (for now).

Is the Firebase server temporarily overloaded or shut down for maintenance, but will be available soon?

No. This is essentially the same as without a network connection.

Permission denied (due to security rules)?

Yes, it really will cause an error for the completion handler.

Does the database location not exist?

No, this will not cause an error for the listener to terminate.

If isConnected is true, does this also mean that all changed data has been synchronized with the server? those. does connected also mean synchronized?

No no. .info/connected will work with true when connected to the database.

If not, how can an application determine the global synchronization status of an entire Firebase client?

There is currently no way to determine if your local data is updated on the server.

Is there a special /.info/synchronized location?

No, such a place does not exist.

Does the application need to manually track the result of successful / unsuccessful synchronization of each onComplete callback?

It depends on the use case. But if you just want to know when all your records are being executed, enter a dummy value and wait for it to complete. Because Firebase writes in order, at this point you can be sure that you received other events.

+6
source

Source: https://habr.com/ru/post/1242568/


All Articles