From bdc7669ff933ce04ec32258e18e2009a4256785d Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Mon, 29 Aug 2022 14:18:02 -0700 Subject: [PATCH 1/6] Fixed onDisconnect logic to use deviceId --- presence-firestore/README.md | 2 +- presence-firestore/firebase.json | 24 +++++++++++- presence-firestore/functions/index.js | 26 ++++++++++--- presence-firestore/public/index.js | 53 ++++++++++++++++----------- 4 files changed, 75 insertions(+), 30 deletions(-) diff --git a/presence-firestore/README.md b/presence-firestore/README.md index 803fc1ce6..cf2de43bc 100644 --- a/presence-firestore/README.md +++ b/presence-firestore/README.md @@ -38,7 +38,7 @@ To deploy the sample to your Firebase app, 1. Run `npm install` to install dependencies for the server-side [functions](functions/) as detailed above. 2. From this top-level sample directory, deploy the `Realtime Database` trigger defined in [functions](functions/) to `Firebase Functions` and the [public](public/) directory app to `Firebase Hosting`. -Assumimg you've created a Firebase application called `firebase-example-123` (make sure it's upgraded to the Spark plan and that `Anonymous Authentication` are enabled). +Assumimg you've created a Firebase application called `firebase-example-123` (make sure it's upgraded to the Blaze plan and that `Anonymous Authentication` are enabled). ```sh firebase use --add firebase-example-123 diff --git a/presence-firestore/firebase.json b/presence-firestore/firebase.json index 8759d50d8..b78b3f009 100644 --- a/presence-firestore/firebase.json +++ b/presence-firestore/firebase.json @@ -1,3 +1,25 @@ { - "hosting": {"public": "public"} + "hosting": { + "public": "public" + }, + "functions": { + "source": "functions" + }, + "emulators": { + "functions": { + "port": 5001 + }, + "hosting": { + "port": 5000 + }, + "ui": { + "enabled": true + }, + "firestore": { + "port": 8080 + }, + "database": { + "port": 9000 + } + } } diff --git a/presence-firestore/functions/index.js b/presence-firestore/functions/index.js index 7cc4633ab..f34f83346 100644 --- a/presence-firestore/functions/index.js +++ b/presence-firestore/functions/index.js @@ -18,6 +18,7 @@ const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); +functions.logger.log(process.env); // Since this code will be running in the Cloud Functions environment // we call initialize Firestore without any arguments because it @@ -26,14 +27,18 @@ const firestore = admin.firestore(); // Create a new function which is triggered on changes to /status/{uid} // Note: This is a Realtime Database trigger, *not* Firestore. -exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate( +exports.onUserStatusChanged = functions.database.ref('/status/{uid}/devices/{deviceId}').onUpdate( async (change, context) => { // Get the data written to Realtime Database const eventStatus = change.after.val(); + const deviceKey = context.params.deviceId; + // Then use other event data to create a reference to the // corresponding Firestore document. - const userStatusFirestoreRef = firestore.doc(`status/${context.params.uid}`); + const userFirestoreRef = firestore.doc(`status/${context.params.uid}`); + const userDeviceCollectionRef = userFirestoreRef.collection(`devices`); + const userStatusFirestoreRef = userDeviceCollectionRef.doc(deviceKey); // It is likely that the Realtime Database change that triggered // this event has already been overwritten by a fast change in @@ -41,17 +46,26 @@ exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate( // and compare the timestamps. const statusSnapshot = await change.after.ref.once('value'); const status = statusSnapshot.val(); - functions.logger.log(status, eventStatus); + // functions.logger.log(status, eventStatus); // If the current timestamp for this data is newer than // the data that triggered this event, we exit this function. if (status.last_changed > eventStatus.last_changed) { return null; } - // Otherwise, we convert the last_changed field to a Date eventStatus.last_changed = new Date(eventStatus.last_changed); - // ... and write it to Firestore. - return userStatusFirestoreRef.set(eventStatus); + if(status.state === 'offline') { + // ... and write it to Firestore. + await userStatusFirestoreRef.delete(); + + if ((await userDeviceCollectionRef.get()).empty) { + return userFirestoreRef.delete(); + } + } else { + await userFirestoreRef.set({ uid: context.params.uid }); + return userStatusFirestoreRef.set(status.devices[deviceKey]); + } + return null; }); // [END presence_sync_function] diff --git a/presence-firestore/public/index.js b/presence-firestore/public/index.js index 76094eeb6..ad9ce9403 100644 --- a/presence-firestore/public/index.js +++ b/presence-firestore/public/index.js @@ -21,7 +21,8 @@ function rtdb_presence() { // Create a reference to this user's specific status node. // This is where we will store data about being online/offline. - var userStatusDatabaseRef = firebase.database().ref('/status/' + uid); + // `push()` will add a new key to the user's path, acting as a deviceId + var deviceStatusDatabaseRef = firebase.database().ref('/status/' + uid + '/devices').push(); // We'll create two constants which we will write to // the Realtime database when this device is offline @@ -49,7 +50,7 @@ function rtdb_presence() { // method to add a set which will only trigger once this // client has disconnected by closing the app, // losing internet, or any other means. - userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { + deviceStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { // The promise returned from .onDisconnect().set() will // resolve as soon as the server acknowledges the onDisconnect() // request, NOT once we've actually disconnected: @@ -57,17 +58,18 @@ function rtdb_presence() { // We can now safely set ourselves as 'online' knowing that the // server will mark us as offline once we lose connection. - userStatusDatabaseRef.set(isOnlineForDatabase); + deviceStatusDatabaseRef.set(isOnlineForDatabase); }); }); // [END rtdb_presence] } -function rtdb_and_local_fs_presence() { +async function rtdb_and_local_fs_presence() { // [START rtdb_and_local_fs_presence] // [START_EXCLUDE] var uid = firebase.auth().currentUser.uid; - var userStatusDatabaseRef = firebase.database().ref('/status/' + uid); + var deviceStatusDatabaseRef = await firebase.database().ref('/status/' + uid + '/devices').push(); + var deviceId = deviceStatusDatabaseRef.key; var isOfflineForDatabase = { state: 'offline', @@ -80,34 +82,39 @@ function rtdb_and_local_fs_presence() { }; // [END_EXCLUDE] - var userStatusFirestoreRef = firebase.firestore().doc('/status/' + uid); + var userFirestoreRef = firebase.firestore().collection('status').doc(uid); + await userFirestoreRef.set({ uid }); // In case the user's id document doesn't already exist. + + var devicesCollectionRef = userFirestoreRef.collection('devices'); + var deviceStatusFirestoreRef = devicesCollectionRef.doc(deviceId); + // Firestore uses a different server timestamp value, so we'll // create two more constants for Firestore state. - var isOfflineForFirestore = { - state: 'offline', - last_changed: firebase.firestore.FieldValue.serverTimestamp(), - }; - var isOnlineForFirestore = { state: 'online', last_changed: firebase.firestore.FieldValue.serverTimestamp(), }; - firebase.database().ref('.info/connected').on('value', function(snapshot) { - if (snapshot.val() == false) { - // 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.' - userStatusFirestoreRef.set(isOfflineForFirestore); + firebase.database().ref('.info/connected').on('value', async function(snapshot) { + if (snapshot.val() === false) { + // Instead of simply returning, we'll also remove the device id from the user, + // and if no devices are left, we should delete the user as well. + // This ensures that our Firestore cache is aware that the device and/or user has been deleted + deviceStatusFirestoreRef.delete(); + const deviceCollection = await devicesCollectionRef.get(); + if(deviceCollection.empty) { + userFirestoreRef.delete(); + } return; }; - userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { - userStatusDatabaseRef.set(isOnlineForDatabase); + deviceStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { + deviceStatusDatabaseRef.set(isOnlineForDatabase); // We'll also add Firestore set here for when we come online. - userStatusFirestoreRef.set(isOnlineForFirestore); + userFirestoreRef.set({ uid }); + deviceStatusFirestoreRef.set(isOnlineForFirestore); }); }); // [END rtdb_and_local_fs_presence] @@ -116,7 +123,7 @@ function rtdb_and_local_fs_presence() { function fs_listen() { // [START fs_onsnapshot] userStatusFirestoreRef.onSnapshot(function(doc) { - var isOnline = doc.data().state == 'online'; + var isOnline = !doc.empty; // ... use isOnline }); // [END fs_onsnapshot] @@ -126,9 +133,9 @@ function fs_listen_online() { var history = document.querySelector('#history'); // [START fs_onsnapshot_online] firebase.firestore().collection('status') - .where('state', '==', 'online') .onSnapshot(function(snapshot) { snapshot.docChanges().forEach(function(change) { + console.log(change); if (change.type === 'added') { var msg = 'User ' + change.doc.id + ' is online.'; console.log(msg); @@ -147,6 +154,8 @@ function fs_listen_online() { }); // [END fs_onsnapshot_online] } +firebase.database().useEmulator("localhost", 9000); +firebase.firestore().useEmulator("localhost", 8080); firebase.auth().signInAnonymously().then(function() { rtdb_and_local_fs_presence(); From 0e8fccc3163a13978e29ff043409e20f39a93a78 Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Mon, 29 Aug 2022 15:42:32 -0700 Subject: [PATCH 2/6] Removed unnecessary changes --- presence-firestore/firebase.json | 24 +----------------------- presence-firestore/functions/index.js | 3 +-- presence-firestore/public/index.js | 3 --- 3 files changed, 2 insertions(+), 28 deletions(-) diff --git a/presence-firestore/firebase.json b/presence-firestore/firebase.json index b78b3f009..8759d50d8 100644 --- a/presence-firestore/firebase.json +++ b/presence-firestore/firebase.json @@ -1,25 +1,3 @@ { - "hosting": { - "public": "public" - }, - "functions": { - "source": "functions" - }, - "emulators": { - "functions": { - "port": 5001 - }, - "hosting": { - "port": 5000 - }, - "ui": { - "enabled": true - }, - "firestore": { - "port": 8080 - }, - "database": { - "port": 9000 - } - } + "hosting": {"public": "public"} } diff --git a/presence-firestore/functions/index.js b/presence-firestore/functions/index.js index f34f83346..c0d070773 100644 --- a/presence-firestore/functions/index.js +++ b/presence-firestore/functions/index.js @@ -18,7 +18,6 @@ const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); -functions.logger.log(process.env); // Since this code will be running in the Cloud Functions environment // we call initialize Firestore without any arguments because it @@ -46,7 +45,7 @@ exports.onUserStatusChanged = functions.database.ref('/status/{uid}/devices/{dev // and compare the timestamps. const statusSnapshot = await change.after.ref.once('value'); const status = statusSnapshot.val(); - // functions.logger.log(status, eventStatus); + functions.logger.log(status, eventStatus); // If the current timestamp for this data is newer than // the data that triggered this event, we exit this function. if (status.last_changed > eventStatus.last_changed) { diff --git a/presence-firestore/public/index.js b/presence-firestore/public/index.js index ad9ce9403..a168a7b82 100644 --- a/presence-firestore/public/index.js +++ b/presence-firestore/public/index.js @@ -135,7 +135,6 @@ function fs_listen_online() { firebase.firestore().collection('status') .onSnapshot(function(snapshot) { snapshot.docChanges().forEach(function(change) { - console.log(change); if (change.type === 'added') { var msg = 'User ' + change.doc.id + ' is online.'; console.log(msg); @@ -154,8 +153,6 @@ function fs_listen_online() { }); // [END fs_onsnapshot_online] } -firebase.database().useEmulator("localhost", 9000); -firebase.firestore().useEmulator("localhost", 8080); firebase.auth().signInAnonymously().then(function() { rtdb_and_local_fs_presence(); From 2cdfa44104bd045b6794e2dec3ed43b9607e8d0b Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Tue, 30 Aug 2022 16:01:37 -0700 Subject: [PATCH 3/6] Renamed some variables --- presence-firestore/functions/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/presence-firestore/functions/index.js b/presence-firestore/functions/index.js index c0d070773..15209d4c5 100644 --- a/presence-firestore/functions/index.js +++ b/presence-firestore/functions/index.js @@ -37,7 +37,7 @@ exports.onUserStatusChanged = functions.database.ref('/status/{uid}/devices/{dev // corresponding Firestore document. const userFirestoreRef = firestore.doc(`status/${context.params.uid}`); const userDeviceCollectionRef = userFirestoreRef.collection(`devices`); - const userStatusFirestoreRef = userDeviceCollectionRef.doc(deviceKey); + const userDeviceStatusFirestoreRef = userDeviceCollectionRef.doc(deviceKey); // It is likely that the Realtime Database change that triggered // this event has already been overwritten by a fast change in @@ -56,14 +56,14 @@ exports.onUserStatusChanged = functions.database.ref('/status/{uid}/devices/{dev if(status.state === 'offline') { // ... and write it to Firestore. - await userStatusFirestoreRef.delete(); + await userDeviceStatusFirestoreRef.delete(); if ((await userDeviceCollectionRef.get()).empty) { return userFirestoreRef.delete(); } } else { await userFirestoreRef.set({ uid: context.params.uid }); - return userStatusFirestoreRef.set(status.devices[deviceKey]); + return userDeviceStatusFirestoreRef.set(status.devices[deviceKey]); } return null; }); From b0ddd63cd56fd8fc0916b69d7f9f4e68e55338d1 Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 31 Aug 2022 09:17:00 -0700 Subject: [PATCH 4/6] Addressed comment --- presence-firestore/functions/index.js | 16 ++++++------ presence-firestore/public/index.js | 36 +++++++++++++-------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/presence-firestore/functions/index.js b/presence-firestore/functions/index.js index 15209d4c5..6a3cf7282 100644 --- a/presence-firestore/functions/index.js +++ b/presence-firestore/functions/index.js @@ -24,20 +24,20 @@ admin.initializeApp(); // detects authentication from the environment. const firestore = admin.firestore(); -// Create a new function which is triggered on changes to /status/{uid} +// Create a new function which is triggered on changes to /status/{uid}/sessions/{sessionId} // Note: This is a Realtime Database trigger, *not* Firestore. -exports.onUserStatusChanged = functions.database.ref('/status/{uid}/devices/{deviceId}').onUpdate( +exports.onUserStatusChanged = functions.database.ref('/status/{uid}/sessions/{sessionId}').onUpdate( async (change, context) => { // Get the data written to Realtime Database const eventStatus = change.after.val(); - const deviceKey = context.params.deviceId; + const sessionId = context.params.sessionId; // Then use other event data to create a reference to the // corresponding Firestore document. const userFirestoreRef = firestore.doc(`status/${context.params.uid}`); - const userDeviceCollectionRef = userFirestoreRef.collection(`devices`); - const userDeviceStatusFirestoreRef = userDeviceCollectionRef.doc(deviceKey); + const userSessionCollectionRef = userFirestoreRef.collection('sessions'); + const sessionStatusFirestoreRef = userSessionCollectionRef.doc(sessionId); // It is likely that the Realtime Database change that triggered // this event has already been overwritten by a fast change in @@ -56,14 +56,14 @@ exports.onUserStatusChanged = functions.database.ref('/status/{uid}/devices/{dev if(status.state === 'offline') { // ... and write it to Firestore. - await userDeviceStatusFirestoreRef.delete(); + await sessionStatusFirestoreRef.delete(); - if ((await userDeviceCollectionRef.get()).empty) { + if ((await userSessionCollectionRef.get()).empty) { return userFirestoreRef.delete(); } } else { await userFirestoreRef.set({ uid: context.params.uid }); - return userDeviceStatusFirestoreRef.set(status.devices[deviceKey]); + return sessionStatusFirestoreRef.set(status.sessions[sessionId]); } return null; }); diff --git a/presence-firestore/public/index.js b/presence-firestore/public/index.js index a168a7b82..2b23c3d7d 100644 --- a/presence-firestore/public/index.js +++ b/presence-firestore/public/index.js @@ -21,11 +21,11 @@ function rtdb_presence() { // Create a reference to this user's specific status node. // This is where we will store data about being online/offline. - // `push()` will add a new key to the user's path, acting as a deviceId - var deviceStatusDatabaseRef = firebase.database().ref('/status/' + uid + '/devices').push(); + // `push()` will add a new key to the user's path, acting as a sessionId + var sessionStatusDatabaseRef = firebase.database().ref('/status/' + uid + '/sessions').push(); // We'll create two constants which we will write to - // the Realtime database when this device is offline + // the Realtime database when this session is offline // or online. var isOfflineForDatabase = { state: 'offline', @@ -50,7 +50,7 @@ function rtdb_presence() { // method to add a set which will only trigger once this // client has disconnected by closing the app, // losing internet, or any other means. - deviceStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { + sessionStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { // The promise returned from .onDisconnect().set() will // resolve as soon as the server acknowledges the onDisconnect() // request, NOT once we've actually disconnected: @@ -58,7 +58,7 @@ function rtdb_presence() { // We can now safely set ourselves as 'online' knowing that the // server will mark us as offline once we lose connection. - deviceStatusDatabaseRef.set(isOnlineForDatabase); + sessionStatusDatabaseRef.set(isOnlineForDatabase); }); }); // [END rtdb_presence] @@ -68,8 +68,8 @@ async function rtdb_and_local_fs_presence() { // [START rtdb_and_local_fs_presence] // [START_EXCLUDE] var uid = firebase.auth().currentUser.uid; - var deviceStatusDatabaseRef = await firebase.database().ref('/status/' + uid + '/devices').push(); - var deviceId = deviceStatusDatabaseRef.key; + var sessionStatusDatabaseRef = await firebase.database().ref('/status/' + uid + '/sessions').push(); + var sessionId = sessionStatusDatabaseRef.key; var isOfflineForDatabase = { state: 'offline', @@ -85,8 +85,8 @@ async function rtdb_and_local_fs_presence() { var userFirestoreRef = firebase.firestore().collection('status').doc(uid); await userFirestoreRef.set({ uid }); // In case the user's id document doesn't already exist. - var devicesCollectionRef = userFirestoreRef.collection('devices'); - var deviceStatusFirestoreRef = devicesCollectionRef.doc(deviceId); + var sessionsCollectionRef = userFirestoreRef.collection('sessions'); + var sessionStatusFirestoreRef = sessionsCollectionRef.doc(sessionId); // Firestore uses a different server timestamp value, so we'll @@ -98,23 +98,23 @@ async function rtdb_and_local_fs_presence() { firebase.database().ref('.info/connected').on('value', async function(snapshot) { if (snapshot.val() === false) { - // Instead of simply returning, we'll also remove the device id from the user, - // and if no devices are left, we should delete the user as well. - // This ensures that our Firestore cache is aware that the device and/or user has been deleted - deviceStatusFirestoreRef.delete(); - const deviceCollection = await devicesCollectionRef.get(); - if(deviceCollection.empty) { + // 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(); + const sessionCollection = await sessionsCollectionRef.get(); + if(sessionCollection.empty) { userFirestoreRef.delete(); } return; }; - deviceStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { - deviceStatusDatabaseRef.set(isOnlineForDatabase); + sessionStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { + sessionStatusDatabaseRef.set(isOnlineForDatabase); // We'll also add Firestore set here for when we come online. userFirestoreRef.set({ uid }); - deviceStatusFirestoreRef.set(isOnlineForFirestore); + sessionStatusFirestoreRef.set(isOnlineForFirestore); }); }); // [END rtdb_and_local_fs_presence] From 60e0d0568eada68c26ac32e45be30eb8cfeac9e3 Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 31 Aug 2022 10:18:43 -0700 Subject: [PATCH 5/6] Fixed offline mode bug --- presence-firestore/functions/index.js | 4 ++-- presence-firestore/public/index.js | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/presence-firestore/functions/index.js b/presence-firestore/functions/index.js index 6a3cf7282..952b84739 100644 --- a/presence-firestore/functions/index.js +++ b/presence-firestore/functions/index.js @@ -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; }); diff --git a/presence-firestore/public/index.js b/presence-firestore/public/index.js index 2b23c3d7d..3fef3ec5b 100644 --- a/presence-firestore/public/index.js +++ b/presence-firestore/public/index.js @@ -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(); From 066cbe2708aa46f009791f394f8a06a506fcf865 Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 31 Aug 2022 10:20:44 -0700 Subject: [PATCH 6/6] Removed unnecessary code --- presence-firestore/public/index.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/presence-firestore/public/index.js b/presence-firestore/public/index.js index 3fef3ec5b..26ef00fcd 100644 --- a/presence-firestore/public/index.js +++ b/presence-firestore/public/index.js @@ -70,7 +70,6 @@ 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', @@ -106,15 +105,13 @@ async function rtdb_and_local_fs_presence() { // 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().then(() => console.log('session deletion completed')); + sessionStatusFirestoreRef.delete(); const sessionCollection = await sessionsCollectionRef.get(); if(sessionCollection.empty) { - userFirestoreRef.delete().then(() => console.log('user deletion completed')); + userFirestoreRef.delete(); } return; }; - const sessionCollection = await sessionsCollectionRef.get(); - console.log(sessionCollection); sessionStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { sessionStatusDatabaseRef.set(isOnlineForDatabase); @@ -160,11 +157,6 @@ 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();