2

I want to change user status to show either he is online or not. I want to change user status to false in database when User close application or when he loses connection with server. enter image description here

As a method is available named as onDisconnect() .I have used that method to update user status by using following code .


HashMap<String,Object>   user_online_status=new HashMap<String,Object>(); 
user_online_status.put("online",true); 
DatabaseReference firebaseDatabase=FirebaseDatabase.getInstance().getReference().child("Users").child(userId);  

  firebaseDatabase.updateChildren(user_online_status); 
  //then to show  user offline 
  user_online_status.put("online",false);
  firebaseDatabase.onDisconnect().updateChildren(user_online_status); 

I do that task but as it is on client side and If we want to monitor user connection with server and when connection is terminated node should be updated by Server Instead of Client.How can we change node value from server as User lose connection with server?

1

1 Answer 1

13

There are two ways the user can get disconnected from the Firebase Database.

  1. a clean disconnect, where the client sends a signal to the server before it disconnects.
  2. a dirty (for lack of a better term) disconnect, where the connection gets closed before the client can send a signal.

In the case of a clean disconnect, your onDisconnect handlers will immediately fire and thus your database will immediately be updated.

In the case of a dirty disconnect, Firebase depends on the socket layer to signal when the remote client is gone. This may take anywhere up to a few minutes. But eventually the server will detect/decide that the client is gone, and your onDisconnect handlers will fire.


A small note in your data structure: you that there is a 1:1 relation between a user and a connection. That is unfortunately not the case.

  • A user may be connected from multiple devices. If they now disconnect from one of those devices, the onDisconnect from that device will set online to false while they may still be connected on another device.
  • Mobile devices/networks have a habit of going through occasional disconnect/reconnect cycles. This means that you may have multiple connections, even on a single device. In case of a dirty disconnect, the onDisconnect handler may be fired much later, when you've already set online to true for the new connection. In such a case, your lingering onDisconnect handler will set online to false while the user may already be reconnected.

All this is to say that you should not rely on having a 1:1 relation between a user and their connection(s). The samples in the Firebase documentation treat connections as a collection and assume that the user is connected as long as there is any "connect ID" (generated by push()) left for that user. I recommend you do the same to prevent hard to debug race conditions and connection problems.

Sign up to request clarification or add additional context in comments.

2 Comments

That's precisely what onDisconnect handlers do. It just may take a few minutes before the server detects that the client is gone.
Sir! If I make a mechanism with an attribute that a user is already in his account from a device and prevent others to come in . As you said mobile devices have habit of occasional disconnect and reconnect and We overcome that issue by Firebase Persistence and by making dataReferences Synchronized.But What if user himself is going to turned off data connection while on the other hand app was running then How can we run code on Firebase server on user lose connection for specific time?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.