EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 446 forks source link

Manage online offline status #500

Open dbeulen opened 6 years ago

dbeulen commented 6 years ago

Is it possible to use the onDisconnect feature described on this website?

https://firebase.google.com/docs/database/web/offline-capabilities


// any time that connectionsRef's value is null (i.e. has no children) I am offline
var myConnectionsRef = firebase.database().ref('users/joe/connections');

// stores the timestamp of my last disconnect (the last time I was seen online)
var lastOnlineRef = firebase.database().ref('users/joe/lastOnline');

var connectedRef = firebase.database().ref('.info/connected');
connectedRef.on('value', function(snap) {
  if (snap.val() === true) {
    // We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
    var con = myConnectionsRef.push();

    // When I disconnect, remove this device
    con.onDisconnect().remove();

    // Add this device to my connections list
    // this value could contain info about the device or a timestamp too
    con.set(true);

    // When I disconnect, update the last time I was seen online
    lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
  }
});```
Dashue commented 6 years ago

This is how I'm using it, is this what you're looking for?

        firebase.addValueEventListener(data => {
            if (data.value) {
                console.log('Status: Online');
            } else {
                console.log('Status: Offline');
            }

        }, '.info/connected');

Although if you're trying to build a presence system, you might want to look at this link, as what you can do in the offline callback is limited (as you're offline): https://firebase.googleblog.com/2013/06/how-to-build-presence-system.html

dbeulen commented 6 years ago

Thank you for your reaction,

I already saw that website, and i'm trying to keep a database value if a user is online or offline. To do so i need these lines: (From the link)

var userRef = new Firebase('https://<demo>.firebaseio.com/presence/' + userid);
userRef.onDisconnect().set('☆ offline');

Do you also use this function?

Dashue commented 6 years ago

Sorry to say I haven't. Think @EddyVerbruggen might be able to shed some more light on this, as it's beyond my limited abilities.

AkshatGiri commented 6 years ago

Did someone figure out a way to use onDisconnect?

alexZaicev commented 5 years ago

Any updates on the topic? I have tried to use onDisconnect() with set/update/remove, but it throws that omDisconnect is not a function...

EddyVerbruggen commented 5 years ago

Indeed it isn't a function because it's not currently implemented (nor documented).