Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Created January 22, 2018 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wajahatkarim3/a2b48029b65c0bbf83464e6bef7d83f7 to your computer and use it in GitHub Desktop.
Save wajahatkarim3/a2b48029b65c0bbf83464e6bef7d83f7 to your computer and use it in GitHub Desktop.
Firebase Cloud Function for User Presence Demo
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const firestore = functions.firestore;
exports.onUserStatusChange = functions.database
.ref('/status/{userId}')
.onUpdate(event => {
var db = admin.firestore();
//const usersRef = firestore.document('/users/' + event.params.userId);
const usersRef = db.collection("users");
var snapShot = event.data;
return event.data.ref.once('value')
.then(statusSnap => snapShot.val())
.then(status => {
if (status === 'offline'){
usersRef
.doc(event.params.userId)
.set({
online: false,
last_active: Date.now()
}, {merge: true});
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment