Created
January 22, 2018 15:56
-
-
Save wajahatkarim3/a2b48029b65c0bbf83464e6bef7d83f7 to your computer and use it in GitHub Desktop.
Firebase Cloud Function for User Presence Demo
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
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