Skip to content

Add RTDB offline snippets #98

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

Merged
merged 5 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
121 changes: 121 additions & 0 deletions database-next/offline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]

import { initializeApp } from "firebase/app";

const firebaseApp = initializeApp({
apiKey: '### FIREBASE API KEY ###',
appId: '### FIREBASE APP ID ###',
projectId: '### FIREBASE PROJECT ID ###'
});

function onDisconnectSimple() {
// [START rtdb_ondisconnect_simple]
const { getDatabase } = require("firebase/database");

const db = getDatabase(firebaseApp);
const presenceRef = db.ref("disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnect().set("I disconnected!");
// [END rtdb_ondisconnect_simple]
}

function onDisconnectCallback() {
const { getDatabase } = require("firebase/database");

const db = getDatabase(firebaseApp);
const presenceRef = db.ref("disconnectmessage");

// [START rtdb_ondisconnect_callback]
presenceRef.onDisconnect().remove((err) => {
if (err) {
console.error("could not establish onDisconnect event", err);
}
});
// [END rtdb_ondisconnect_callback]
}

function onDisconnectCancel() {
const { getDatabase } = require("firebase/database");

const db = getDatabase(firebaseApp);
const presenceRef = db.ref("disconnectmessage");

// [START rtdb_ondisconnect_cancel]
const onDisconnectRef = presenceRef.onDisconnect();
onDisconnectRef.set("I disconnected");
// some time later when we change our minds
onDisconnectRef.cancel();
// [END rtdb_ondisconnect_cancel]
}

function detectConnectionState() {
// [START rtdb_detect_connection_state]
const { getDatabase } = require("firebase/database");

const db = getDatabase(firebaseApp);
const connectedRef = db.ref(".info/connected");
connectedRef.on("value", (snap) => {
if (snap.val() === true) {
console.log("connected");
} else {
console.log("not connected");
}
});
// [END rtdb_detect_connection_state]
}

function setServerTimestamp() {
// [START rtdb_set_server_timestamp]
const { getDatabase, ServerValue } = require("firebase/database");

const db = getDatabase(firebaseApp);
const userLastOnlineRef = db.ref("users/joe/lastOnline");
userLastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
// [END rtdb_set_server_timestamp]
}

function estimateClockSkew() {
// [START rtdb_estimate_clock_skew]
const { getDatabase } = require("firebase/database");

const db = getDatabase(firebaseApp);
const offsetRef = db.ref(".info/serverTimeOffset");
offsetRef.on("value", (snap) => {
const offset = snap.val();
const estimatedServerTimeMs = new Date().getTime() + offset;
});
// [END rtdb_estimate_clock_skew]
}

function samplePresenceApp() {
// [START rtdb_sample_presence_app]
const { getDatabase, ServerValue } = require("firebase/database");

// Since I can connect from multiple devices or browser tabs, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
const db = getDatabase(firebaseApp);
const myConnectionsRef = db.ref('users/joe/connections');

// stores the timestamp of my last disconnect (the last time I was seen online)
const lastOnlineRef = db.ref('users/joe/lastOnline');

const connectedRef = db.ref('.info/connected');
connectedRef.on('value', (snap) => {
if (snap.val() === true) {
// We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
const con = myConnectionsRef.push();

// When I disconnect, remove this device
con.onDisconnect().remove();

// Add this device to my connections list
// this value could contain info about the device or a timestamp too
con.set(true);

// When I disconnect, update the last time I was seen online
lastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
}
});
// [END rtdb_sample_presence_app]
}
95 changes: 95 additions & 0 deletions database/offline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// These samples are intended for Web so this import would normally be
// done in HTML however using modules here is more convenient for
// ensuring sample correctness offline.
import firebase from "firebase/app";
import "firebase/database";

function onDisconnectSimple() {
// [START rtdb_ondisconnect_simple]
var presenceRef = firebase.database().ref("disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnect().set("I disconnected!");
// [END rtdb_ondisconnect_simple]
}

function onDisconnectCallback() {
var presenceRef = firebase.database().ref("disconnectmessage");

// [START rtdb_ondisconnect_callback]
presenceRef.onDisconnect().remove((err) => {
if (err) {
console.error("could not establish onDisconnect event", err);
}
});
// [END rtdb_ondisconnect_callback]
}

function onDisconnectCancel() {
var presenceRef = firebase.database().ref("disconnectmessage");

// [START rtdb_ondisconnect_cancel]
var onDisconnectRef = presenceRef.onDisconnect();
onDisconnectRef.set("I disconnected");
// some time later when we change our minds
onDisconnectRef.cancel();
// [END rtdb_ondisconnect_cancel]
}

function detectConnectionState() {
// [START rtdb_detect_connection_state]
var connectedRef = firebase.database().ref(".info/connected");
connectedRef.on("value", (snap) => {
if (snap.val() === true) {
console.log("connected");
} else {
console.log("not connected");
}
});
// [END rtdb_detect_connection_state]
}

function setServerTimestamp() {
// [START rtdb_set_server_timestamp]
var userLastOnlineRef = firebase.database().ref("users/joe/lastOnline");
userLastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
// [END rtdb_set_server_timestamp]
}

function estimateClockSkew() {
// [START rtdb_estimate_clock_skew]
var offsetRef = firebase.database().ref(".info/serverTimeOffset");
offsetRef.on("value", (snap) => {
var offset = snap.val();
var estimatedServerTimeMs = new Date().getTime() + offset;
});
// [END rtdb_estimate_clock_skew]
}

function samplePresenceApp() {
// [START rtdb_sample_presence_app]
// Since I can connect from multiple devices or browser tabs, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
var myConnectionsRef = firebase.database().ref('users/joe/connections');

// stores the timestamp of my last disconnect (the last time I was seen online)
var lastOnlineRef = firebase.database().ref('users/joe/lastOnline');

var connectedRef = firebase.database().ref('.info/connected');
connectedRef.on('value', (snap) => {
if (snap.val() === true) {
// We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
var con = myConnectionsRef.push();

// When I disconnect, remove this device
con.onDisconnect().remove();

// Add this device to my connections list
// this value could contain info about the device or a timestamp too
con.set(true);

// When I disconnect, update the last time I was seen online
lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
}
});
// [END rtdb_sample_presence_app]
}
18 changes: 18 additions & 0 deletions snippets/database-next/offline/rtdb_detect_connection_state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This snippet file was generated by processing the source file:
// ./database-next/offline.js
//
// To make edits to the snippets in this file, please edit the source

// [START rtdb_detect_connection_state_modular]
import { getDatabase } from "firebase/database";

const db = getDatabase(firebaseApp);
const connectedRef = db.ref(".info/connected");
connectedRef.on("value", (snap) => {
if (snap.val() === true) {
console.log("connected");
} else {
console.log("not connected");
}
});
// [END rtdb_detect_connection_state_modular]
15 changes: 15 additions & 0 deletions snippets/database-next/offline/rtdb_estimate_clock_skew.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This snippet file was generated by processing the source file:
// ./database-next/offline.js
//
// To make edits to the snippets in this file, please edit the source

// [START rtdb_estimate_clock_skew_modular]
import { getDatabase } from "firebase/database";

const db = getDatabase(firebaseApp);
const offsetRef = db.ref(".info/serverTimeOffset");
offsetRef.on("value", (snap) => {
const offset = snap.val();
const estimatedServerTimeMs = new Date().getTime() + offset;
});
// [END rtdb_estimate_clock_skew_modular]
12 changes: 12 additions & 0 deletions snippets/database-next/offline/rtdb_ondisconnect_callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This snippet file was generated by processing the source file:
// ./database-next/offline.js
//
// To make edits to the snippets in this file, please edit the source

// [START rtdb_ondisconnect_callback_modular]
presenceRef.onDisconnect().remove((err) => {
if (err) {
console.error("could not establish onDisconnect event", err);
}
});
// [END rtdb_ondisconnect_callback_modular]
11 changes: 11 additions & 0 deletions snippets/database-next/offline/rtdb_ondisconnect_cancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This snippet file was generated by processing the source file:
// ./database-next/offline.js
//
// To make edits to the snippets in this file, please edit the source

// [START rtdb_ondisconnect_cancel_modular]
const onDisconnectRef = presenceRef.onDisconnect();
onDisconnectRef.set("I disconnected");
// some time later when we change our minds
onDisconnectRef.cancel();
// [END rtdb_ondisconnect_cancel_modular]
13 changes: 13 additions & 0 deletions snippets/database-next/offline/rtdb_ondisconnect_simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This snippet file was generated by processing the source file:
// ./database-next/offline.js
//
// To make edits to the snippets in this file, please edit the source

// [START rtdb_ondisconnect_simple_modular]
import { getDatabase } from "firebase/database";

const db = getDatabase(firebaseApp);
const presenceRef = db.ref("disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnect().set("I disconnected!");
// [END rtdb_ondisconnect_simple_modular]
34 changes: 34 additions & 0 deletions snippets/database-next/offline/rtdb_sample_presence_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This snippet file was generated by processing the source file:
// ./database-next/offline.js
//
// To make edits to the snippets in this file, please edit the source

// [START rtdb_sample_presence_app_modular]
import { getDatabase, ServerValue } from "firebase/database";

// Since I can connect from multiple devices or browser tabs, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
const db = getDatabase(firebaseApp);
const myConnectionsRef = db.ref('users/joe/connections');

// stores the timestamp of my last disconnect (the last time I was seen online)
const lastOnlineRef = db.ref('users/joe/lastOnline');

const connectedRef = db.ref('.info/connected');
connectedRef.on('value', (snap) => {
if (snap.val() === true) {
// We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
const con = myConnectionsRef.push();

// When I disconnect, remove this device
con.onDisconnect().remove();

// Add this device to my connections list
// this value could contain info about the device or a timestamp too
con.set(true);

// When I disconnect, update the last time I was seen online
lastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
}
});
// [END rtdb_sample_presence_app_modular]
12 changes: 12 additions & 0 deletions snippets/database-next/offline/rtdb_set_server_timestamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This snippet file was generated by processing the source file:
// ./database-next/offline.js
//
// To make edits to the snippets in this file, please edit the source

// [START rtdb_set_server_timestamp_modular]
import { getDatabase, ServerValue } from "firebase/database";

const db = getDatabase(firebaseApp);
const userLastOnlineRef = db.ref("users/joe/lastOnline");
userLastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP);
// [END rtdb_set_server_timestamp_modular]