Firebase onDisconnect how to trigger other function



I am reading firebase doc on onDisconnect but it didn’t quite mention the operation i am looking for.

Goal: when a connection is disconnected, I want to run a function that updates other data that is not related to this connection data.

My code is below:

let PlayerRef = database.ref("/players");
let MessageRef = database.ref("/messages");
let con=playersRef.child(0).push('test');   
con.onDisconnect().remove(); // yes i want to remove that current connection
// BUT I also want to update the MessageRef that is 
//using the data in the "con", how can I acheive this???

How would I achieve this?


You can attach an onDisconnect() handler to any location in your database and remove or write data to that location.

But realize that onDisconnect() works by sending the write operation to the Firebase servers up front. So you can write any value, as long as that value can be calculated on the client when you attach the onDisconnect listener.

Answered By – Frank van Puffelen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More