Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed onDisconnect logic to use deviceId #1010

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed offline mode bug
  • Loading branch information
maneesht committed Aug 31, 2022
commit 60e0d0568eada68c26ac32e45be30eb8cfeac9e3
4 changes: 2 additions & 2 deletions presence-firestore/functions/index.js
Original file line number Diff line number Diff line change
@@ -61,9 +61,9 @@ exports.onUserStatusChanged = functions.database.ref('/status/{uid}/sessions/{se
if ((await userSessionCollectionRef.get()).empty) {
return userFirestoreRef.delete();
}
} else {
} else { // TODO(mtewani): Do we really need to handle this?
await userFirestoreRef.set({ uid: context.params.uid });
return sessionStatusFirestoreRef.set(status.sessions[sessionId]);
return sessionStatusFirestoreRef.set(status);
}
return null;
});
16 changes: 14 additions & 2 deletions presence-firestore/public/index.js
Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ async function rtdb_and_local_fs_presence() {
var uid = firebase.auth().currentUser.uid;
var sessionStatusDatabaseRef = await firebase.database().ref('/status/' + uid + '/sessions').push();
var sessionId = sessionStatusDatabaseRef.key;
console.log('my ID', sessionId);

var isOfflineForDatabase = {
state: 'offline',
@@ -96,18 +97,24 @@ async function rtdb_and_local_fs_presence() {
last_changed: firebase.firestore.FieldValue.serverTimestamp(),
};

// When a user goes offline, it reads from the cache to know how many sessions are left.
// This listener is to ensure that the full session cache is populated and can be read from when offline.
sessionsCollectionRef.onSnapshot(() => {});

firebase.database().ref('.info/connected').on('value', async function(snapshot) {
if (snapshot.val() === false) {
// Instead of simply returning, we'll also remove the session id from the user,
// and if no sessions are left, we should delete the user as well.
// This ensures that our Firestore cache is aware that the session and/or user has been deleted
sessionStatusFirestoreRef.delete();
sessionStatusFirestoreRef.delete().then(() => console.log('session deletion completed'));
const sessionCollection = await sessionsCollectionRef.get();
if(sessionCollection.empty) {
userFirestoreRef.delete();
userFirestoreRef.delete().then(() => console.log('user deletion completed'));
}
return;
};
const sessionCollection = await sessionsCollectionRef.get();
console.log(sessionCollection);

sessionStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() {
sessionStatusDatabaseRef.set(isOnlineForDatabase);
@@ -153,6 +160,11 @@ function fs_listen_online() {
});
// [END fs_onsnapshot_online]
}
const db = firebase.database();
const firestore = firebase.firestore();
db.useEmulator("localhost", 9000);
firestore.useEmulator("localhost", 8080);


firebase.auth().signInAnonymously().then(function() {
rtdb_and_local_fs_presence();