Skip to content

Commit 3bf1495

Browse files
authored
Add RTDB offline snippets (#98)
1 parent 1c8d114 commit 3bf1495

9 files changed

+331
-0
lines changed

database-next/offline.js

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// [SNIPPET_REGISTRY disabled]
2+
// [SNIPPETS_SEPARATION enabled]
3+
4+
import { initializeApp } from "firebase/app";
5+
6+
const firebaseApp = initializeApp({
7+
apiKey: '### FIREBASE API KEY ###',
8+
appId: '### FIREBASE APP ID ###',
9+
projectId: '### FIREBASE PROJECT ID ###'
10+
});
11+
12+
function onDisconnectSimple() {
13+
// [START rtdb_ondisconnect_simple]
14+
const { getDatabase } = require("firebase/database");
15+
16+
const db = getDatabase(firebaseApp);
17+
const presenceRef = db.ref("disconnectmessage");
18+
// Write a string when this client loses connection
19+
presenceRef.onDisconnect().set("I disconnected!");
20+
// [END rtdb_ondisconnect_simple]
21+
}
22+
23+
function onDisconnectCallback() {
24+
const { getDatabase } = require("firebase/database");
25+
26+
const db = getDatabase(firebaseApp);
27+
const presenceRef = db.ref("disconnectmessage");
28+
29+
// [START rtdb_ondisconnect_callback]
30+
presenceRef.onDisconnect().remove((err) => {
31+
if (err) {
32+
console.error("could not establish onDisconnect event", err);
33+
}
34+
});
35+
// [END rtdb_ondisconnect_callback]
36+
}
37+
38+
function onDisconnectCancel() {
39+
const { getDatabase } = require("firebase/database");
40+
41+
const db = getDatabase(firebaseApp);
42+
const presenceRef = db.ref("disconnectmessage");
43+
44+
// [START rtdb_ondisconnect_cancel]
45+
const onDisconnectRef = presenceRef.onDisconnect();
46+
onDisconnectRef.set("I disconnected");
47+
// some time later when we change our minds
48+
onDisconnectRef.cancel();
49+
// [END rtdb_ondisconnect_cancel]
50+
}
51+
52+
function detectConnectionState() {
53+
// [START rtdb_detect_connection_state]
54+
const { getDatabase } = require("firebase/database");
55+
56+
const db = getDatabase(firebaseApp);
57+
const connectedRef = db.ref(".info/connected");
58+
connectedRef.on("value", (snap) => {
59+
if (snap.val() === true) {
60+
console.log("connected");
61+
} else {
62+
console.log("not connected");
63+
}
64+
});
65+
// [END rtdb_detect_connection_state]
66+
}
67+
68+
function setServerTimestamp() {
69+
// [START rtdb_set_server_timestamp]
70+
const { getDatabase, ServerValue } = require("firebase/database");
71+
72+
const db = getDatabase(firebaseApp);
73+
const userLastOnlineRef = db.ref("users/joe/lastOnline");
74+
userLastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
75+
// [END rtdb_set_server_timestamp]
76+
}
77+
78+
function estimateClockSkew() {
79+
// [START rtdb_estimate_clock_skew]
80+
const { getDatabase } = require("firebase/database");
81+
82+
const db = getDatabase(firebaseApp);
83+
const offsetRef = db.ref(".info/serverTimeOffset");
84+
offsetRef.on("value", (snap) => {
85+
const offset = snap.val();
86+
const estimatedServerTimeMs = new Date().getTime() + offset;
87+
});
88+
// [END rtdb_estimate_clock_skew]
89+
}
90+
91+
function samplePresenceApp() {
92+
// [START rtdb_sample_presence_app]
93+
const { getDatabase, ServerValue } = require("firebase/database");
94+
95+
// Since I can connect from multiple devices or browser tabs, we store each connection instance separately
96+
// any time that connectionsRef's value is null (i.e. has no children) I am offline
97+
const db = getDatabase(firebaseApp);
98+
const myConnectionsRef = db.ref('users/joe/connections');
99+
100+
// stores the timestamp of my last disconnect (the last time I was seen online)
101+
const lastOnlineRef = db.ref('users/joe/lastOnline');
102+
103+
const connectedRef = db.ref('.info/connected');
104+
connectedRef.on('value', (snap) => {
105+
if (snap.val() === true) {
106+
// We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
107+
const con = myConnectionsRef.push();
108+
109+
// When I disconnect, remove this device
110+
con.onDisconnect().remove();
111+
112+
// Add this device to my connections list
113+
// this value could contain info about the device or a timestamp too
114+
con.set(true);
115+
116+
// When I disconnect, update the last time I was seen online
117+
lastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
118+
}
119+
});
120+
// [END rtdb_sample_presence_app]
121+
}

database/offline.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// These samples are intended for Web so this import would normally be
2+
// done in HTML however using modules here is more convenient for
3+
// ensuring sample correctness offline.
4+
import firebase from "firebase/app";
5+
import "firebase/database";
6+
7+
function onDisconnectSimple() {
8+
// [START rtdb_ondisconnect_simple]
9+
var presenceRef = firebase.database().ref("disconnectmessage");
10+
// Write a string when this client loses connection
11+
presenceRef.onDisconnect().set("I disconnected!");
12+
// [END rtdb_ondisconnect_simple]
13+
}
14+
15+
function onDisconnectCallback() {
16+
var presenceRef = firebase.database().ref("disconnectmessage");
17+
18+
// [START rtdb_ondisconnect_callback]
19+
presenceRef.onDisconnect().remove((err) => {
20+
if (err) {
21+
console.error("could not establish onDisconnect event", err);
22+
}
23+
});
24+
// [END rtdb_ondisconnect_callback]
25+
}
26+
27+
function onDisconnectCancel() {
28+
var presenceRef = firebase.database().ref("disconnectmessage");
29+
30+
// [START rtdb_ondisconnect_cancel]
31+
var onDisconnectRef = presenceRef.onDisconnect();
32+
onDisconnectRef.set("I disconnected");
33+
// some time later when we change our minds
34+
onDisconnectRef.cancel();
35+
// [END rtdb_ondisconnect_cancel]
36+
}
37+
38+
function detectConnectionState() {
39+
// [START rtdb_detect_connection_state]
40+
var connectedRef = firebase.database().ref(".info/connected");
41+
connectedRef.on("value", (snap) => {
42+
if (snap.val() === true) {
43+
console.log("connected");
44+
} else {
45+
console.log("not connected");
46+
}
47+
});
48+
// [END rtdb_detect_connection_state]
49+
}
50+
51+
function setServerTimestamp() {
52+
// [START rtdb_set_server_timestamp]
53+
var userLastOnlineRef = firebase.database().ref("users/joe/lastOnline");
54+
userLastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
55+
// [END rtdb_set_server_timestamp]
56+
}
57+
58+
function estimateClockSkew() {
59+
// [START rtdb_estimate_clock_skew]
60+
var offsetRef = firebase.database().ref(".info/serverTimeOffset");
61+
offsetRef.on("value", (snap) => {
62+
var offset = snap.val();
63+
var estimatedServerTimeMs = new Date().getTime() + offset;
64+
});
65+
// [END rtdb_estimate_clock_skew]
66+
}
67+
68+
function samplePresenceApp() {
69+
// [START rtdb_sample_presence_app]
70+
// Since I can connect from multiple devices or browser tabs, we store each connection instance separately
71+
// any time that connectionsRef's value is null (i.e. has no children) I am offline
72+
var myConnectionsRef = firebase.database().ref('users/joe/connections');
73+
74+
// stores the timestamp of my last disconnect (the last time I was seen online)
75+
var lastOnlineRef = firebase.database().ref('users/joe/lastOnline');
76+
77+
var connectedRef = firebase.database().ref('.info/connected');
78+
connectedRef.on('value', (snap) => {
79+
if (snap.val() === true) {
80+
// We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
81+
var con = myConnectionsRef.push();
82+
83+
// When I disconnect, remove this device
84+
con.onDisconnect().remove();
85+
86+
// Add this device to my connections list
87+
// this value could contain info about the device or a timestamp too
88+
con.set(true);
89+
90+
// When I disconnect, update the last time I was seen online
91+
lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
92+
}
93+
});
94+
// [END rtdb_sample_presence_app]
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/offline.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_detect_connection_state_modular]
7+
import { getDatabase } from "firebase/database";
8+
9+
const db = getDatabase(firebaseApp);
10+
const connectedRef = db.ref(".info/connected");
11+
connectedRef.on("value", (snap) => {
12+
if (snap.val() === true) {
13+
console.log("connected");
14+
} else {
15+
console.log("not connected");
16+
}
17+
});
18+
// [END rtdb_detect_connection_state_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/offline.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_estimate_clock_skew_modular]
7+
import { getDatabase } from "firebase/database";
8+
9+
const db = getDatabase(firebaseApp);
10+
const offsetRef = db.ref(".info/serverTimeOffset");
11+
offsetRef.on("value", (snap) => {
12+
const offset = snap.val();
13+
const estimatedServerTimeMs = new Date().getTime() + offset;
14+
});
15+
// [END rtdb_estimate_clock_skew_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/offline.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_ondisconnect_callback_modular]
7+
presenceRef.onDisconnect().remove((err) => {
8+
if (err) {
9+
console.error("could not establish onDisconnect event", err);
10+
}
11+
});
12+
// [END rtdb_ondisconnect_callback_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/offline.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_ondisconnect_cancel_modular]
7+
const onDisconnectRef = presenceRef.onDisconnect();
8+
onDisconnectRef.set("I disconnected");
9+
// some time later when we change our minds
10+
onDisconnectRef.cancel();
11+
// [END rtdb_ondisconnect_cancel_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/offline.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_ondisconnect_simple_modular]
7+
import { getDatabase } from "firebase/database";
8+
9+
const db = getDatabase(firebaseApp);
10+
const presenceRef = db.ref("disconnectmessage");
11+
// Write a string when this client loses connection
12+
presenceRef.onDisconnect().set("I disconnected!");
13+
// [END rtdb_ondisconnect_simple_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/offline.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_sample_presence_app_modular]
7+
import { getDatabase, ServerValue } from "firebase/database";
8+
9+
// Since I can connect from multiple devices or browser tabs, we store each connection instance separately
10+
// any time that connectionsRef's value is null (i.e. has no children) I am offline
11+
const db = getDatabase(firebaseApp);
12+
const myConnectionsRef = db.ref('users/joe/connections');
13+
14+
// stores the timestamp of my last disconnect (the last time I was seen online)
15+
const lastOnlineRef = db.ref('users/joe/lastOnline');
16+
17+
const connectedRef = db.ref('.info/connected');
18+
connectedRef.on('value', (snap) => {
19+
if (snap.val() === true) {
20+
// We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
21+
const con = myConnectionsRef.push();
22+
23+
// When I disconnect, remove this device
24+
con.onDisconnect().remove();
25+
26+
// Add this device to my connections list
27+
// this value could contain info about the device or a timestamp too
28+
con.set(true);
29+
30+
// When I disconnect, update the last time I was seen online
31+
lastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
32+
}
33+
});
34+
// [END rtdb_sample_presence_app_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./database-next/offline.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START rtdb_set_server_timestamp_modular]
7+
import { getDatabase, ServerValue } from "firebase/database";
8+
9+
const db = getDatabase(firebaseApp);
10+
const userLastOnlineRef = db.ref("users/joe/lastOnline");
11+
userLastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
12+
// [END rtdb_set_server_timestamp_modular]

0 commit comments

Comments
 (0)