Last active
August 29, 2015 14:17
-
-
Save chrishan17/3d38ca5e3f8953628d7d to your computer and use it in GitHub Desktop.
firebase_presence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// since I can connect from multiple devices or browser tabs, we store each connection instance separately | |
// any time that connectionsRef's value is null (i.e. has no children) I am offline | |
var myConnectionsRef = new Firebase('https://<your-firebase>.firebaseio.com/users/joe/connections'); | |
// stores the timestamp of my last disconnect (the last time I was seen online) | |
var lastOnlineRef = new Firebase('https://<your-firebase>.firebaseio.com/users/joe/lastOnline'); | |
var connectedRef = new Firebase('https://<your-firebase>.firebaseio.com/.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) | |
// add this device to my connections list | |
// this value could contain info about the device or a timestamp too | |
var con = myConnectionsRef.push(true); | |
// when I disconnect, remove this device | |
con.onDisconnect().remove(); | |
// when I disconnect, update the last time I was seen online | |
lastOnlineRef.onDisconnect().set(Firebase.ServerValue.TIMESTAMP); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment