I have created a cloud function that listens for deletions from the firebase realtime database. It works perfectly when there is only one deletion -- the console confirms the removal of that item.
However, I noticed that if multiple deletes happen simultaneously in my realtime database, the cloud function is only triggered for one of those deletions. What could be causing this issue?
Here's the scenario: I open two browser tabs on the same website. Each time I access my web app on each tab, I create a new entry in the realtime database and get a reference using push
. Later, when I close both tabs by closing the browser window, it triggers the onDelete
event on my references which should remove them from the database via remove
. While I can see both entries being deleted from the database, only one of the deletions is recognized by my cloud function.
The Cloud Function
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.onPresenceRemove = functions.database.ref('/status/app').onDelete( (snapshot, context) => {
const original = snapshot.val();
console.error( 'snapshot', original, snapshot, context );
return true;
});
The JavaScript Realtime Database Code
presenceDoc = firebase.database().ref( '/status/app' );
firebase.database().ref( '.info/connected' ).on( 'value', function( snapshot ) {
if ( snapshot.val() === true ) {
presenceRef = presenceDoc.push( userId );
presenceRef.onDisconnect().remove();
}
});