Home > OS >  Firestore - change document value after certain time
Firestore - change document value after certain time

Time:11-30

We have developed the chat application in flutter and the backend is Firestore, which has a one-to-one and group chat functionality and is working fine as expected, including the ability to track if the user is online or offline with last seen date/time.

When a user is online, we mark it as online along with updating the last seen date/time. If the user sends another message, we also update the last seen date/time again just to ensure the latest last seen date/time updated.

Also, We mark it as offline when the user closes or loses focus from application.

Problem Statement: We could not be able to update that information in the FireStore database when the user disconnects the phone from mobile data or WiFi connection.

Any suggestions would be greatly appreciated.

Regards, Hi Ten

CodePudding user response:

For this its easier using realtime database using onDisconnect

Firebase Database clients provide simple primitives that you can use to write to the database when a client disconnects from the Firebase 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.

Here is a simple example of writing data upon disconnection by using the onDisconnect primitive: more info

 OnDisconnect ref = FirebaseDatabase.instance.reference().onDisconnect();
    ref.set({"status": "offline"});

CodePudding user response:

You want to manage the user presence system in the Application i.e. whether the user is online or offline. Firestore SDK currently does not have a user presence management system as available with Firebase Realtime Database. There is an existing issue for this in Public Issue Tracker which you can star to receive any updates there.

Luckily, you can still achieve the user presence system while using Firestore as the backend database. For that you have to implement Cloud Functions with Firebase Realtime Database as mentioned here.

To connect Cloud Firestore to Firebase Realtime Database's native presence feature, use Cloud Functions.

Use Realtime Database to report connection status, then use Cloud Functions to mirror that data into Cloud Firestore.

As you have mentioned that you can not change the database to Realtime Database now, the best possible option for you is to implement the above.

The blog Managing user presence with Firestore explains the same in detail.

  • Related