Firebase connection check online offline status in Android

12,026

Solution 1

To solve this, you can create a new node in your Firebase Realtime Database to hold all online users, so when the user opens the application, you'll immediately add his id to this newly created node. Then, if you want to check if the user is online, just check if his id exists in the list.

You can also add a new property named isOnline for each user in your database and then update it accordingly.

For that, I recommend you using Firebase's built-in onDisconnect() method. It enables you to predefine an operation that will happen as soon as the client becomes disconnected.

See Firebase documentation.

You can also detect the connection state of the user. For many presence-related features, it is useful for your app to know when it is online or offline. Firebase Realtime Database provides a special location at /.info/connected which is updated every time the Firebase Realtime Database client's connection state changes. Here is an example also from the official documentation:

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
    connectedRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            boolean connected = snapshot.getValue(Boolean.class);
            if (connected) {
                System.out.println("connected");
            } else {
                System.out.println("not connected");
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            System.err.println("Listener was cancelled");
        }
});

Solution 2

Though this is more than a year late, to clear up the confusion. Alex's question would like to implement a live chat scenario in which each user can view everyone's online status at their ends or on their devices. A simple solution be to create a node where all users would inject their online status each. e.g.

    //say your realtime database has the child `online_statuses`
    DatabaseReference online_status_all_users = FirebaseDatabase.getInstance().getReference().child("online_statuses");

    //on each user's device when connected they should indicate e.g. `linker` should tell everyone he's snooping around
    online_status_all_users.child("@linker").setValue("online");
    //also when he's not doing any snooping or if snooping goes bad he should also tell
    online_status_all_users.child("@linker").onDisconnect().setValue("offline")

So if another user, say mario checks for linker from his end he can be sure some snooping around is still ongoing if linker is online i.e.

    DatabaseReference online_status_all_users = FirebaseDatabase.getInstance().getReference().child("online_statuses");
    online_status_all_users.child("@linker").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
        String snooping_status = dataSnapshot.getValue(String.class);
        //mario should decide what to do with linker's snooping status here e.g.
         if(snooping_status.contentEquals("online")){
            //tell linker to stop doing sh*t
         }else{
            //tell linker to do a lot of sh****t
         }     
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
    });
Share:
12,026

Related videos on Youtube

sajidamin
Author by

sajidamin

Updated on June 04, 2022

Comments

  • sajidamin
    sajidamin almost 2 years

    If user turn off both wi-fi, 3g, 4g, and so on and reverse (no internet connection). Firebase database name child connections:(true/false) So, when internet connections, wi-fi, 3g, 4g, and so on are off or missing, the user is offline so he can't be found.

    Remember the two scenarios: Before and After. If user is offline before an other user search him, then he will not displayed in the list result, if user is off-line after an other user search him, then it will display NO MORE AVAILABLE icon on the user

    Kindly some one help me for this problem.

    • Tarun Dholakiya
      Tarun Dholakiya almost 6 years
      Can you please elaborate your question and be specific ?
    • sajidamin
      sajidamin almost 6 years
      just want to check that the user is connected with internet or not and after showing the offline and online user.
  • sajidamin
    sajidamin almost 6 years
    and what can i do for the internet connection
  • Alex Mamo
    Alex Mamo almost 6 years
    You need to go online. You should find an internet spot in order to make changes in your Firebase database.
  • sajidamin
    sajidamin almost 6 years
    how can i do all this senioro can u please send me full source code
  • Alex Mamo
    Alex Mamo almost 6 years
    I don't understand you, a full source code for what? In my answer above, I have explained you how how you can get the state of the user. I recommend you also checking out the documentation and trying it. If you get stuck during a specific step, show what you've tried.
  • Alex Mamo
    Alex Mamo almost 6 years
    Is there everything alright? Have you solved the issue?
  • Sourav Kannantha B
    Sourav Kannantha B almost 3 years
    @AlexMamo thanks for this answer. I had totally missed that section of docs. Btw, in this they have mentioned how to attach a ValueEventListener on .info/connected. Can you tell me when to add this listener and when to remove this in my app.
  • Alex Mamo
    Alex Mamo almost 3 years
    @SouravKannanthaB I think you might be interested in this answer.