Firebase multi-device presence system

In my Firebase-based chat application, I wanted the user to find out if another user is connected to any of the devices (browser / iOS / Android).

I referenced the sample shown on this page . But using this example, I noticed that several connection keys are being created, but not being deleted properly. So I ended up improving the example as follows:

function _managePresence(uid) { // top level presence node var ref = coreService.presence.child(uid); // stores connections from various browsers or devices var myConnectionsRef = ref.child('connections'); // stores the timestamp of my last disconnect (the last time I was seen online) var lastOnlineRef = ref.child('lastOnline'); // this one is special, it tells locally to the device, if I'm connected or not var connectedRef = coreService.connection; // add this device to my connections list var addConnection = function(myConnectionsRef) { // TODO: this value could contain info about the device or a timestamp too var con = myConnectionsRef.push(true); // save the connection key (con.key) locally also console.log("saving connection key", con.key); localStorage.setItem('currentConnectionKey', con.key); // when I disconnect, remove this connection key (from firebase) con.onDisconnect().remove(); }; 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) // retrieve connection key from local storage var connectionKey = localStorage.getItem('currentConnectionKey'); console.log("connection key found", connectionKey); if (connectionKey) { // connection key already exists locally, lets check firebase // NOTE: the use of once here, we don't want on('value') // because when the connection is removed, on('value') will get called // with snap.val() being null and we dont want to create a new connection key in that case // new connection key should only be created when we see a change in connectedRef.on('value') myConnectionsRef.child(connectionKey).once('value', function (snap) { if (snap.val() === true) { // connection key already exists in firebase also, we wont add a new connection key console.log("key already exists locally and in firebase", connectionKey); } else { // this connection key does not exist in firebase, lets create a new connection console.log("key not found in firebase, lets add a new one"); addConnection(myConnectionsRef); } }); } else { console.log("First time, lets addConnection"); addConnection(myConnectionsRef); } // when I disconnect, update the last time I was seen online lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP); } else { console.log("disconnecting..."); } }); } 

To find out if someone is online or not, I just look at the number of children for / presence / uid / connections

  _isOnline() { var ref = coreService.presence.child(this.uid); var myConnectionsRef = ref.child('connections'); var self = this; myConnectionsRef.on('value', function (snap) { if (snap.hasChildren()) self.isON = true; else self.isON = false; }); } _lastOnline() { var ref = coreService.presence.child(this.uid); var lastOnlineRef = ref.child('lastOnline'); var self = this; lastOnlineRef.on( 'value', function (snap) { self.lastOnlineTime = snap.val(); }); } 

Is it right to determine if the user is online? It may be too much code to read, but I would appreciate it if someone could help me check it out.

+1
javascript firebase firebase-database user-presence
source share

No one has answered this question yet.

See similar questions:

10
Firebase onDisconnect not 100% reliable?

or similar:

7649
How do JavaScript locks work?
1966
How can I select an item with multiple classes in jQuery?
1949
How does JavaScript.prototype work?
1690
How does data binding work in AngularJS?
1547
What is the best way to detect a mobile device?
1201
How does the keyword "this" work?
249
How to store and view images on firebase?
4
How to read a Firebase database entry in Firebase cloud features? Index.js?
2
Firebase database database does not work between starts
one
Firebase onDisconnect () fires several times

All Articles