Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any method like onDisconnect() in firestore like there is in realtime database?

I want to check user's online status, in realtime database I used to check this with the help of onDisconnect(), but now I've shifted to firestore and can't find any similar method in that.

like image 333
Kashish Sharma Avatar asked Mar 06 '18 06:03

Kashish Sharma


People also ask

Can I use Realtime Database and firestore at the same time?

You can use both Firebase Realtime Database and Cloud Firestore in your app, and leverage each database solution's benefits to fit your needs. For example, you might want to leverage Realtime Database's support for presence, as outlined in Build Presence in Cloud Firestore.

Is firestore better than Realtime Database?

Cloud Firestore is Firebase's new flagship database for mobile app development. It improves on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales better than the Realtime Database.

What does Firebase firestore () do?

Cloud Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even if the device is offline.

Is Firebase firestore real time?

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing: Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model.


2 Answers

According to this onDisconnect:

The onDisconnect class is most commonly used to manage presence in applications where it is useful to detect how many clients are connected and when other clients disconnect.

To be able to use presence in firestore, you need to connect firestore with realtime firebase(no other way).

Please check this for more info:

https://firebase.google.com/docs/firestore/solutions/presence

like image 97
Peter Haddad Avatar answered Sep 24 '22 06:09

Peter Haddad


NOTE: This solution is not especially efficient

Off the top of my head (read: I haven't thought through the caveats), you could do something like this:

const fiveMinutes = 300000 // five minutes, or whatever makes sense for your app

// "maintain connection"
setInterval(() => {
  userPresenceDoc.set({ online: new Date().getTime() })
}, fiveMinutes)

then, on each client...

userPresenceDoc.onSnapshot(doc => {
  const fiveMinutesAgo = new Date().getTime() - fiveMinutes
  const isOnline = doc.data().online > fiveMinutesAgo
  setUserPresence(isOnline)
})

You'd probably want the code checking for presence to use an interval a little more than the interval used by the code maintaining the connection to account for network lag, etc.

A NOTE ABOUT COST

So, obviously, there could be significant lag between when someone disconnects and when that disconnection is recognized by other clients. You can decrease that lag time by increasing the frequency of writes to Firestore, and thus increasing your costs. Running the numbers, I came out with the following costs for different intervals, assuming a single client connection running continuously for a month:

Interval     Cost/User/Month
----------------------------
10m          $0.007776
 5m          $0.015552
 1m          $0.07776
10s          $0.46656
 5s          $0.93312
 1s          $4.6656

Going with an interval of one second is pretty pricy, at $46,656 a month for a system with 10,000 users who leave the app open all month long. An interval of 10 minutes with the same number of users would only cost $77.76 a month. A more reasonable interval of one minute, 10,000 users, and only four hours of app usage per day per user, rings in at $129.60 / month.

like image 25
Trevor Avatar answered Sep 25 '22 06:09

Trevor