-
-
Save alanbacelar/078460da083fa027ba30de98496c52fb to your computer and use it in GitHub Desktop.
Online Presence with Firebase and Android based off article https://firebase.googleblog.com/2013/06/how-to-build-presence-system.html . Read the article as it explains the whole .onDisconnect().removeValue() nicely.
This file contains hidden or 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
private void initialiseOnlinePresence() { | |
final DatabaseReference onlineRef = databaseReference.child(".info/connected"); | |
final DatabaseReference currentUserRef = databaseReference.child("/presence/" + userId); | |
onlineRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(final DataSnapshot dataSnapshot) { | |
Log.d(TAG, "DataSnapshot:" + dataSnapshot); | |
if (dataSnapshot.getValue(Boolean.class)){ | |
currentUserRef.onDisconnect().removeValue(); | |
currentUserRef.setValue(true); | |
} | |
} | |
@Override | |
public void onCancelled(final DatabaseError databaseError) { | |
Log.d(TAG, "DatabaseError:" + databaseError); | |
} | |
}); | |
final DatabaseReference onlineViewersCountRef = databaseReference.child("/presence"); | |
onlineViewersCountRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(final DataSnapshot dataSnapshot) { | |
Log.d(TAG, "DataSnapshot:" + dataSnapshot); | |
onlineViewerCountTextView.setText(String.valueOf(dataSnapshot.getChildrenCount())); | |
} | |
@Override | |
public void onCancelled(final DatabaseError databaseError) { | |
Log.d(TAG, "DatabaseError:" + databaseError); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment