Skip to content

Commit 867dba2

Browse files
committed
Adds fs_onsnapshot
1 parent 46623db commit 867dba2

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

firestore/presence/public/index.js

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
function rtdb_presence() {
12
// [START rtdb_presence]
23
// Fetch the current user's ID from Firebase Authentication.
34
const uid = firebase.auth().currentUser.uid;
@@ -9,14 +10,14 @@ const userStatusDatabaseRef = firebase.database().ref(`/status/${uid}`);
910
// We'll create two constants which we will write to
1011
// the Realtime database when this device is offline
1112
// or online.
12-
const isOffline = {
13+
const isOfflineForDatabase = {
1314
state: "offline",
14-
last_seen: firebase.database.ServerValue.TIMESTAMP,
15+
last_changed: firebase.database.ServerValue.TIMESTAMP,
1516
};
1617

17-
const isOnline = {
18-
state: "offline",
19-
last_seen: firebase.database.ServerValue.TIMESTAMP,
18+
const isOnlineForDatabase = {
19+
state: "online",
20+
last_changed: firebase.database.ServerValue.TIMESTAMP,
2021
};
2122

2223
// Create a reference to the special ".info/connected" path in
@@ -32,32 +33,75 @@ firebase.database().ref(".info/connected").on("value", function (snapshot) {
3233
// method to add a set which will only trigger once this
3334
// client has disconnected by closing the app,
3435
// losing internet, or any other means.
35-
userStatusDatabaseRef.onDisconnect().set(isOffline).then(function () {
36+
userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function () {
3637
// The promise returned from .onDisconnect().set() will
3738
// resolve as soon as the server acknowledges the onDisconnect()
3839
// request, NOT once we've actually disconnected:
3940
// https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect
4041

4142
// We can now safely set ourselves as "online" knowing that the
4243
// server will mark us as offline once we lose connection.
43-
userStatusDatabaseRef.set(isOnline);
44+
userStatusDatabaseRef.set(isOnlineForDatabase);
4445
});
4546
});
4647
// [END rtdb_presence]
48+
}
4749

50+
function rtdb_and_local_fs_presence() {
4851
// [START rtdb_and_local_fs_presence]
49-
[START_EXCLUDE]
50-
[END_EXCLUDE]
52+
// [START_EXCLUDE]
53+
const uid = firebase.auth().currentUser.uid;
5154
const userStatusDatabaseRef = firebase.database().ref(`/status/${uid}`);
55+
56+
const isOfflineForDatabase = {
57+
state: "offline",
58+
last_changed: firebase.database.ServerValue.TIMESTAMP,
59+
};
60+
61+
const isOnlineForDatabase = {
62+
state: "online",
63+
last_changed: firebase.database.ServerValue.TIMESTAMP,
64+
};
65+
66+
// [END_EXCLUDE]
5267
const userStatusFirestoreRef = firebase.firestore().doc(`/status/${uid}`);
5368

69+
// Firestore uses a different server timestamp value, so we'll
70+
// create two more constants for Firestore state.
71+
const isOfflineForFirestore = {
72+
state: "offline",
73+
last_changed: firebase.firestore.FieldValue.serverTimestamp(),
74+
};
75+
76+
const isOnlineForFirestore = {
77+
state: "online",
78+
last_changed: firebase.firestore.FieldValue.serverTimestamp(),
79+
};
80+
5481
firebase.database().ref(".info/connected").on("value", function (snapshot) {
5582
if (snapshot.val() == false) {
83+
// Instead of simply returning, we'll also set Firestore's state
84+
// to "offline". This ensures that our Firestore cache is aware
85+
// of the switch to "offline."
86+
userStatusFirestoreRef.set(isOfflineForFirestore);
5687
return;
5788
};
5889

59-
userStatusRef.onDisconnect().set(isOffline).then(function () {
60-
userStatusRef.set(isOnline);
90+
userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function () {
91+
userStatusDatabaseRef.set(isOnlineForDatabase);
92+
93+
// We'll also add Firestore set here for when we come online.
94+
userStatusFirestoreRef.set(isOnlineForFirestore);
6195
});
6296
});
6397
// [END rtdb_and_local_fs_presence]
98+
}
99+
100+
function fs_listen() {
101+
// [START fs_onsnapshot]
102+
userStatusFirestoreRef.onSnapshot(function (doc) {
103+
const isOnline = doc.data().state == "online";
104+
// ... use isOnline
105+
});
106+
// [END fs_onsnapshot]
107+
}

0 commit comments

Comments
 (0)