I am currently working on a chat application using Firebase within my Vue.js project. In this setup, I need to display the user's status as either active or inactive. To achieve this, I implemented the solution provided by Firebase at https://firebase.google.com/docs/firestore/solutions/presence. However, I am facing an issue where the Realtime database is being updated but Cloud Firestore is not reflecting these changes. Any assistance in resolving this matter would be greatly appreciated.
let user = this.profile.id;
let base = database.ref("/online_users/" + user);
var store = db.doc('/status/' + user);
var isOfflineForFirestore = {
state: 'offline',
user_id: user,
};
var isOnlineForFirestore = {
state: 'online',
user_id: user,
};
var isOfflineForDatabase = {
state: 'offline',
user_id: user,
};
var isOnlineForDatabase = {
state: 'online',
user_id: user,
};
database.ref('/online_users/'+this.profile.id).on('value', function(snapshot) {
if (snapshot.val().state == 'offline') {
// Instead of simply returning, we'll also set Firestore's state
// to 'offline'. This ensures that our Firestore cache is aware
// of the switch to 'offline.'
store.set(isOfflineForFirestore);
console.log(snapshot.val());
}
base.onDisconnect().set(isOfflineForDatabase).then(function() {
store.set(isOnlineForFirestore);
base.set(isOnlineForDatabase);
});
});