Skip to content

Instantly share code, notes, and snippets.

@davydany
Created August 28, 2018 22:35
Show Gist options
  • Save davydany/9bd42cf8bfc518f0eaf64ba079ebc061 to your computer and use it in GitHub Desktop.
Save davydany/9bd42cf8bfc518f0eaf64ba079ebc061 to your computer and use it in GitHub Desktop.
Snippet showing Firebase working with Electron Background Process
// This is main process of Electron, started as first thing when your
// app starts. It runs through entire life of your application.
// It doesn't have any windows which you can see on screen, but we can open
// window from here.
import path from "path";
import url from "url";
import { app, Menu, protocol } from "electron";
import { devMenuTemplate } from "./menu/dev_menu_template";
import { editMenuTemplate } from "./menu/edit_menu_template";
import createWindow from "./helpers/window";
// Special module holding environment variables which you declared
// in config/env_xxx.json file.
import env from "env";
const firebase = require("firebase");
// Required for side-effects
require("firebase/firestore");
var firebase_config = {
projectId: '<MY_PROJECT_ID>',
apiKey: "<MY_API_KEY>",
authDomain: "<MY_PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<MY_PROJECT_ID>.firebaseio.com",
storageBucket: "<MY_PROJECT_ID>.appspot.com",
};
firebase.initializeApp(firebase_config);
const setApplicationMenu = () => {
const menus = [editMenuTemplate];
if (env.name !== "production") {
menus.push(devMenuTemplate);
}
Menu.setApplicationMenu(Menu.buildFromTemplate(menus));
};
// Save userData in separate folders for each environment.
// Thanks to this you can use production and development versions of the app
// on same machine like those are two separate apps.
if (env.name !== "production") {
const userDataPath = app.getPath("userData");
app.setPath("userData", `${userDataPath} (${env.name})`);
}
app.setAsDefaultProtocolClient
app.on("ready", () => {
setApplicationMenu();
protocol.registerFileProtocol('ddtest', (request, callback) => {
const url = request.url;
console.log("Received Call from ", url);
callback({path: path.normalize(`${__dirname}/${url}`)})
}, (error) => {
if (error) console.error('Failed to register protocol')
})
const mainWindow = createWindow("main", {
width: 1000,
height: 600
});
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "app.html"),
protocol: "file:",
slashes: true
})
);
if (env.name === "development") {
mainWindow.openDevTools();
}
var db = firebase.firestore();
var docRef = db.collection('users').doc('davydany');
docRef.get().then((doc) => {
if (doc.exists) {
console.log('Document.data: ', doc.data());
} else {
console.log('No Such Document');
}
}).catch((err) => {
console.log("Error getting document:", error);
});
docRef.onSnapshot((doc) => {
var source = doc.metadata.hasPendingWrites ? "Local" : "Server";
console.log(source, " data: ", doc.data());
});
});
app.on("window-all-closed", () => {
app.quit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment