1

I'm trying to build a presence system using firebase. The problem is that onDisconnect is not triggering when I close my app or turn off my network connectivity.

enter image description here

My Code:

 FirebaseDatabase firebase=FirebaseDatabase.getInstance();
        firebase.getReference("Users/"+shared.getString("username","")+"/presence").onDisconnect().setValue("offline");
  • 1) How long did you wait? In case of a dirty disconnect it's up to the server to detect that the client is gone, which may take up to a couple of minutes. 2) Can you reproduce the same with `FirebaseDatabase.getInstance().getReference("test").onDisconnect().setValue("offline")`? – Frank van Puffelen Jul 20 '20 at 14:32
  • I have literally waited for 1 hour and still nothing changed. If it really takes like more than 5min for the server to detect client disconnectivity, then it really isn't worth the effort. And yes, I tried this too, ain't working :/ I just knew about cloud firestore not supporting onDiscount handling so I switched to firebase database. – Darkmoon Chief Jul 20 '20 at 14:40
  • Where you say `onDiscount`, do you mean `onDisconnect`? – Frank van Puffelen Jul 20 '20 at 15:08
  • It should not take more than a few minutes, so it seems that the server really thinks it's still connected to your client in that case. You might want to [enabling debug logging](https://firebase.google.com/docs/reference/android/com/google/firebase/database/FirebaseDatabase.html#setLogLevel(com.google.firebase.database.Logger.Level)) to see if the client indeed disconnected. Aside from that, there's isn't much you can do. Are the users that show `"offline"` tests from different devices? – Frank van Puffelen Jul 20 '20 at 15:11
  • omg I don't know why I typed discount instead of disconnect lol. – Darkmoon Chief Jul 20 '20 at 15:12
  • I have doubts concerning the keyword "client". Does it refer to a user who is connected to firebase via firebase authentication or what? Because I'm not using this service. – Darkmoon Chief Jul 20 '20 at 15:14
  • In this case client is the device that uses the Firebase Realtime Database SDK. We use the term client to set it apart from the database server, or from a server-side SDK. – Frank van Puffelen Jul 20 '20 at 15:23
  • oh so it should work for me in this case because I'm obviously using the sdk. Weird. – Darkmoon Chief Jul 20 '20 at 15:31
  • I just want to thank you for your time regardless if my problem is resolved or not. – Darkmoon Chief Jul 20 '20 at 15:31
  • 1
    Not a problem. Let me know what the debug logging in the client shows. I'm not expecting anything, but it's the best way to troubleshoot that I can think of. – Frank van Puffelen Jul 20 '20 at 15:45
  • OMG!!!! I cannot believe I have committed this silly mistake. I'm bashing my head on the wall right now! It turns out that the firebase rules for reading and writing are switched off! It works perfectly fine now, it doesn't even take 5 seconds to trigger the onDisconnect method which is awesome! – Darkmoon Chief Jul 21 '20 at 03:03
  • 1
    Oh wow, nice catch. I didn't think of that, and that is actually a hard problem to catch. Good job! – Frank van Puffelen Jul 21 '20 at 03:25

0 Answers0