Sam
Sam

Reputation: 137

Firebase Functions user offline trigger

I would like to update user's status info when he just lost the connection inside the app. But I cannot trigger when he lose connection with Firebase.

Here some of triggers:

onWrite(), //which triggers when data is created, destroyed, or changed in the Realtime Database.
onCreate(), //which triggers when new data is created in the Realtime Database.
onUpdate(), //which triggers when data is updated in the Realtime Database.
onDelete(), //which triggers when data is deleted from the Realtime Database.

So how to update user's status if he lost the connection? Any hint will be appreciated

Upvotes: 1

Views: 838

Answers (1)

J. Doe
J. Doe

Reputation: 13043

This will help you out: https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect

Create a onWrite event on lets say /disconnected/{user}

Assuming you are coding in Swift, you could use the following code:

dataRef.child("disconnected").onDisconnectUpdateChildValues(["uid" : user.uid])
//ofcourse change dataRef to your reference.

When the user disconnect -> Firebase executes the onDisconnect -> onWrite event get triggered.

Javascript:

ref.onDisconnect().update({onlineState: false,status: "I'm offline."})

Upvotes: 5

Related Questions