Angular V6 Desktop Application With Firebase And Electron

In this article, we will create a simple contact list application in Angular 6 using Firebase as the database and publish it  as a Firebase app.

Later we will convert this app
to a desktop app using electron,  and we will create a stand-alone executable file using electron-packager.

Step 1 - Create a Firebase project and Realtime Database.

For those who are new to Firebase, it is a Backend-as-a-Service — BaaS — that started as a YC11 startup and grew up into a next-generation app-development platform on Google Cloud Platform. Firebase frees developers up to focus crafting fantastic user experiences. You don’t need to manage servers.

You don’t need to write APIs. Firebase is your server, your API and your datastore, all written so generically that you can modify it to suit most needs.

Currently Firebase supports many features as shown below.

Angular V6 Desktop Application with Firebase and Electron
In this project, we use Firebase as a NoSQL database and store our contact list inside one collection named contacts.

We can create a Firebase project now. It'sfree.

Please go to this URL: Firebase Console and log in with your existing Google account. It will load the console, there you can see an add project button. Please click.

Give your project a name and it will create a unique project id automatically. You can change this project id later if needed.

Angular V6 Desktop Application with Firebase and Electron 
 
Now our project is ready and go to dashboard. There you can see a "Add Firebase to you web app" button. As we are creating Angular application we need the Firebase app details to configure.

Angular V6 Desktop Application with Firebase and Electron

Copy the configurations and keep them separately. Later in your Angular app, we will use it. 
It looks like below.

Angular V6 Desktop Application with Firebase and Electron 

We are almost done with Firebase. We need to create a database.  Google launched the beta version of the Cloud Firestore, but in this project, we use a real time database.

Click Develop -> Database -> Create Database

Start in test mode so that we will get Write and Read mode enabled.

Angular V6 Desktop Application with Firebase and Electron

Click Enable button and our database will be created shortly.

Angular V6 Desktop Application with Firebase and Electron 

It currently shows a warning message as our security rules are defined as public and anybody can steal or modify our data. Please dismiss this. 
We can change our security rule later.

We are almost done with our Firebase database and we will use this DB in our Angular application.

Step 2 - Create our Angular application with Angular CLI.

ng new angular6-firebase-electron --spec false
We must install the below packages in our Angular app. We must add the below packages in packages.json file and run npm install command.
  1. "angularfire2""^5.0.0-rc.7",  
  2. "firebase""^5.0.2",  
  3. "ngx-toastr""^8.1.0"  
npm install
We can add the remaining files one by one.

Please download the entire source code from Github and run
npm install. 
  1. "styles": [  
  2.    "src/styles/main.scss",  
  3.    "node_modules/ngx-toastr/toastr.css"  
  4. ],  

We will add the firebase database configurations to environment.prod.ts and environment.tsfile.

Angular V6 Desktop Application with Firebase and Electron 
 
We are controlling our CRUD operations in contact.service.ts (Inside \src\app\contacts\common folder)

contact.service.ts 
  1. import { Injectable } from '@angular/core';  
  2. import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';  
  3. import { Contact } from './contact.model';  
  4. @Injectable()  
  5. export class ContactService {  
  6.    contactList: AngularFireList<any>;  
  7.    selectedContact: Contact = new Contact();  
  8.    constructor(private firebase: AngularFireDatabase) { }  
  9.    // Getting data from firebase db  
  10. getData() {  
  11.    this.contactList = this.firebase.list('contacts');  
  12.    return this.contactList;  
  13. }  
  14. // Inserting data into firebase db  
  15. insertContact(contact: Contact) {  
  16.    this.contactList.push({  
  17.    firstName: contact.firstName,  
  18.    lastName: contact.lastName,  
  19.    phone: contact.phone,  
  20.    email: contact.email  
  21.    });  
  22. }  
  23. // Updating data in firebase db  
  24. updateContact(contact: Contact) {  
  25.    this.contactList.update(contact.$key, {  
  26.    firstName: contact.firstName,  
  27.    lastName: contact.lastName,  
  28.    phone: contact.phone,  
  29.    email: contact.email  
  30.    });  
  31. }  
  32. // Removing an item in firebase db  
  33. removeContact($key: string) {  
  34.    this.contactList.remove($key);  
  35.    }  
  36. }  
We use three components in this app. Component, Components and ComponentList,

We can run the application now.

ng serve
Angular V6 Desktop Application with Firebase and Electron

I am adding a sample contact now.

When we click the save button, data will be saved in the firebase database very quickly. Firebase is very fast as per my experience.

We can check the data in Firebase Console.

Angular V6 Desktop Application with Firebase and Electron
 
Please notice that a new collection (name: contacts) is created with four fields and one record.

Step
3 - Publish our Angular app to Firebase cloud using firebase-tools

Before hosting our application, we need to build 
our angular production version using below npm command.

ng build --prod

Our production version of app is
ready, and it is available in the dist folder.

It is easy to host 
our app in the cloud. Please install firebase-tools using below npm command.

npm install firebase-tools --g

Login to firebase

firebase login

It will ask you to login with your 
existing Google account and allow Firebase CLI to access your Google account.

Angular V6 Desktop Application with Firebase and Electron
Initialize firebase
  1. firebase init
Angular V6 Desktop Application with Firebase and Electron 
 Press y to proceed,

 Angular V6 Desktop Application with Firebase and Electron

You can move the down arrow and press the space bar to select Hosting option and enter.

Angular V6 Desktop Application with Firebase and Electron 

Please enter 
dist as your public directory. (Because as per our angular.json file angular builds the production version to dist folder.)

Angular V6 Desktop Application with Firebase and Electron

Press y to configure

Angular V6 Desktop Application with Firebase and Electron

It asks to overwrite dist/index.html. Please press N. (Very important) Otherwise your app will be over written by firebase default code.
 
Firebase initialization is completed.

We can deploy our app now.

firebase deploy
Angular V6 Desktop Application with Firebase and Electron
 
Our app is ready, and you can check the browser now.

Please note that it is very responsive, when we change the data in firebase console, it will immediately reflect in our app without refreshing. (like SignalR)

Step 4 - Create desktop app from our current Angular project using Electron.

We have co
me to our last step. Create a desktop app from our existing Angular app using Electron.
For those who are new to Electron, it is a framework for creating native applications with web technologies like JavaScript, HTML, and CSS.

It takes care of the hard 
parts, so you can focus on the core of your application. It also supports Angular and React.

For creating an electron app, we must install electron and electron packager in our application.

Add package reference for electron and electron package in package.json and run npm install to install them.

  1. "electron""^1.7.6",  
  2. "electron-packager""^9.1.0" 
Add the below two entries inside package.json file below scripts section.
  1. "electron""electron .",  
  2. "electron-build""ng build --prod && electron ."  

We can build the electron app using npm run electron-build command.

For electron, main.js is the entry point. We need to create that file in root folder.

main.js
 

  1. const {  
  2.     app,  
  3.     BrowserWindow  
  4. } = require('electron')  
  5. let win;  
  6.   
  7. function createWindow() {  
  8.     // Create the browser window.  
  9.     win = new BrowserWindow({  
  10.         width: 800,  
  11.         height: 600,  
  12.         backgroundColor: '#ffffff',  
  13.         icon: `file://${__dirname}/dist/assets/logo.png`  
  14.     })  
  15.     win.loadURL(`file://${__dirname}/dist/index.html`)  
  16.     //// uncomment below to open the DevTools.  
  17.     // win.webContents.openDevTools()  
  18.     // Event when the window is closed.  
  19.     win.on('closed'function() {  
  20.         win = null  
  21.     })  
  22. }  
  23. // Create window on electron intialization  
  24. app.on('ready', createWindow)  
  25. // Quit when all windows are closed.  
  26. app.on('window-all-closed'function() {  
  27.     // On macOS specific close process  
  28.     if (process.platform !== 'darwin') {  
  29.         app.quit()  
  30.     }  
  31. })  
  32. app.on('activate'function() {  
  33.     // macOS specific close process  
  34.     if (win === null) {  
  35.         createWindow()  
  36.     }  
  37. })  
Inside this file, there is a createWindow function and this will define all the properties of our desktop app.

We must modify the package.json with a main entry as below.

Angular V6 Desktop Application with Firebase and Electron
We are ready to run our electron app.

npm run electron-build

Our desktop application is ready and
opened as below.
 
Angular V6 Desktop Application with Firebase and Electron 
 
Please note that our already created contact is visible in this app and you can add/update more details.

This is just run as an electron emulator, we can create our windows app using this simple command.

electron-packager . --platform=win32

electron-packager is a tool used to create win32 package. It also runs in 64-bit operating systems without any issues.
It will take some time to build our windows application. After that, our fully working windows app is ready.
We have now created one web app and a windows app from the same Angular code. We used Firebase realtime database as backend and hosted in firebaseapp.com. All are free.

I hope you enjoyed the simple steps to create Firebase app and Electron app from same Angular code.

You can download the entire source code from Github repo.

I have setup a sample live demo for this application in firebaseapp.com LIVE DEMO.

Happy coding with Angular, Firebase and Electron.