0

I am working on chat application using Firebase, i need online, offline and last seen status of the user whom i am chatting with. I will get this status when user is connected to internet, when user doesn't have internet then it won't be updated in Firebase and it still shows online to offline user.

How to update Firebase database instantly when there is no internet connectivity?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Amar Giram
  • 157
  • 1
  • 2
  • 12

2 Answers2

1

How to update firebase database instantly when there is no internet connectivity?

There is no way in which you can update a Firebase database while you are offline. When there is no internet connectivity on user's device it means that the user cannot reach Firebase servers, with other words no changes will be made on Firbease servers but, if you are using the following line of code:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

It means that you'll be able to query your the database even if you are offline. This is happening because Firebase creates a local copy of your database. Every change that is made while you are offline, will be updated on Firebase servers once you are back online. To be more clear, every client that is using a Firebase database and uses setPersistenceEnabled(true) maintains it's own internal (local) version of the database. When data is updated, it is first written to this local version of the database.

There is also a post on which I have answered a few minutes ago and I think you might be interested.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Then how can i know the user i am chatting with is online or offline? – Amar Giram May 23 '18 at 06:49
  • how whats app or other chat apps track whether another user went offline when there is no internet connectivity. – Amar Giram May 23 '18 at 06:56
  • 1
    As explained in this **[post](https://stackoverflow.com/questions/50480943/firebase-connection-check-online-offline-status-in-android)**. Change the property of the user from `isOnline: false` to `isOnline: true` once the user regain internet connection and vice versa, right? – Alex Mamo May 23 '18 at 06:56
  • In the exact same way I have explained above. – Alex Mamo May 23 '18 at 06:57
  • I am using same to save online or offline status of the user in firebase database. But as you explained above we cannot update firebase when there is no internet connectivity. So how can i know the user i am chatting with is offline currently. when user is connected to internet it updates "isOnline" as true, when i lost my internet connectivity it remains "isOnline" as true rather than i should update to "false". – Amar Giram May 23 '18 at 07:11
  • When the user you are chating with becomes offline (you can know it using my answer from that post) the `isOnline` property of that user becomes `false`. So you need to check constantly the state of the user by adding value event listener on that property, right? – Alex Mamo May 23 '18 at 07:16
  • When the user I am chatting with goes offline because of internet connectivity how the firebase database will be updated "isOnline" true to false? – Amar Giram May 23 '18 at 07:30
  • See [here](https://firebase.google.com/docs/database/android/offline-capabilities#how-ondisconnect-works). – Alex Mamo May 23 '18 at 07:39
  • UserReference.child("online").onDisconnect().setValue("false"); i did this code still database not updating the value when there is no internet connectio. – Amar Giram May 23 '18 at 10:22
  • You are using a `String` for false or the `boolean` false? If it is a boolean you should you: `UserReference.child("online").onDisconnect().setValue(false);`. – Alex Mamo May 23 '18 at 10:32
  • I tried both cases but database updated only an only when internet is connected. – Amar Giram May 23 '18 at 10:37
  • Check again the doc. Should work. [This](https://firebase.google.com/docs/database/android/offline-capabilities#server-timestamps) is also how you can set a timestamp. – Alex Mamo May 23 '18 at 10:49
  • onDisconnect() method only execute when internet connected. i think its not right way to update database when there is no internet connection.any other solution have you? – Amar Giram May 23 '18 at 11:05
  • Is the correct way for doing that, as the official documentation says, not me. If doesn't work, it means that you are doing something wrong and unfortunately, there is no other way, as far as I know. – Alex Mamo May 23 '18 at 11:18
  • Read this: `In realtime applications it is often useful to detect when clients connect/disconnect. For example, you may want to mark a user as 'offline' when their client disconnects. Firebase Database clients provide simple primitives that you can use to write to the database when a client disconnects from the database servers. These updates occur whether the client disconnects cleanly or not, so you can rely on them to clean up data even if a connection is dropped or a client crashes. All write operations, including setting, updating, and removing, can be performed upon a disconnection.` – Alex Mamo May 23 '18 at 11:19
  • Is there everything alright? Can I help you with other informations? – Alex Mamo May 24 '18 at 05:55
  • its working fine we have to update database onDisconnect() but firebase takes few minute updating data. – Amar Giram May 24 '18 at 13:47
1

Try to add timestamp of both users to firebase at regular intervals .. if connection is down the last active time is shown .. so at both sides check the timestamp difference . If it is greater than some value then display as one user is offline .. makes sure both sides uses GMT time as standard

Jinson Paul
  • 481
  • 6
  • 17