Angular 6 FireBase Gallery <ServerLess>
Ever wanted to build a server less image gallery application with out wanting to deal with client side — server side programming separately? Follow this tutorial to find out how.
In this project, I want to share with you the approach I took to develop a beautiful gallery application using just a client side framework (Angular) and some google cloud services. Please check how I developed the same application using MEAN stack and Amazon Web Services here and here where I dealt with client side and server side logic separately.
As you might be already familiar, Angular is a TypeScript-based open-source front-end web application platform led by the Angular Team at Google and by a community of individuals and corporations. Firebase is a mobile and web application development platform developed by Firebase, Inc. in 2011, then acquired by Google in 2014. Google consistently develops the product adding more features to it to make developer’s life easy. Check out whats new with ’18 firebase. To understand what new with Angular 6, please refer to this article from walkingtree.tech.
In order to follow this tutorial, you are expected to have some familiarity with developing applications in Angular framework and some understanding on how cloud services work.
By the end of this tutorial, you should be able to build a gallery application which lets you
1) Upload an image onto Google storage and add an entry to your application database (FireStore). You would also learn to develop a progress bar which shows the progress of the uploading.
2) View / maximize the images which you have uploaded.
3) Delete the image from Google storage and your application database.
I am going to divide this tutorial into 3 sections Set Up, Development & Deployment for better readability.
Set Up:
Let’s start with setting up FireBase. Create an account with firebase if you have not done it already and log into your console.
Create a new project.
Go to project settings
Under ‘General’ settings, scroll down on this page to find ‘Add Firebase to web app’
Take a note of the api config details. These details are used in your Angular code to communicate with this app on firebase.
Let’s get into setting up the project on your local machine. I used VS Code as my IDE for development. You need to have Node JS installed on your machine. Use this link to download and install it. Install Angular CLI. Create new angular project ‘ng new firebase-angular-gallery’ . and install firebase-tools globally and install firebase and angularfire2 for this project. Initialize git repository ( assuming you already have git on your system . If not, you need to download & install git first ).
npm install @angular/cli -g
ng new firebase-angular-gallery
npm install firebase-tools -g
npm install angularfire2 firebase --save
git init
Create 2 components, one for gallery and other for notifications and 1 service for notifications.
ng g c gallery --spec false
ng g c notification --spec falseng g s notification-services --spec false
The gallery component contains our core application logic, while the notifications component contains code related to modal notifications.
Make sure you import all necessary modules for the project in app.module.ts. This completes our set up. Lets look into some code now.
Development:
Here is the full repository of the application on git hub.
Let’s understand what is happening in the code.
Let’s take a look at the gallery component html file. This is the first component loaded from app.component file. This has code for uploading a new image, viewing the uploaded images , maximizing the image and deleting the image.
I used MDbootstrap (material design for twitter’s bootstrap), flex and angular’s ‘ngFor’ to display multiple images per row. When I use ‘col-md-3’ class inside a row class and subject that to a loop, I can create a view of displaying multiple rows equally spaced as per ‘col-md-3’.
I used ‘bootstrap’s card’ to display the image in the form of a card. To know about it visit this link. I used modals to view the image in larger size. When the user clicks on the image or clicks on ‘Maximize’ button, I would send image details to the modal. I used ‘notification component’ and ‘notification services’ to do this. More details on notifications comes later in this tutorial.
Let me switch topics and explain why I chose these technologies. I chose Angular framework because of its scalability and its powerful CLI. Also, the language is backed by the tech giant ‘Google’. The team does incredible job responding to developer queries and they constantly upgrade the language to make it even better. One of the popular mobile app development frame works ‘ionic’ uses angular framework.
The reason I chose ‘Firebase’ for database, storage and hosting is a no brainer. Firebase is a product from google aswell. The integration is seem less between Angular and Firebase as they are all from same eco system. The documentation is very thorough and there is lot of help available online to learn about it yourself.
To learn more about Angular frame work, please visit https://angular.io/.
To learn more about Firebase, please check this.
Lets get back to code now :)
On selecting a file, I called “onFileChange(event)” function which previews the image and calls another function startUpload(event.target.files) which starts uploading the file to google storage.
For more details on firebase storage functions, please visit this link.
For more details on firebase firestore functions, please visit this link.
Lets now take a look at how I set up notifications.
Below are some of the notifications implemented in this project. I made use of Twitter Bootstrap’s modals to do this.
This completes the development part of the tutorial. Lets see how we deploy the application on google hosting services.
Deployment:
Build the angular project. Make sure the “outputPath” is set to “dist” in angular.json.
ng build
Login to firebase
firebase login
Initialize firebase
This completes the tutorial. Please keep in mind that the application which you are going to develop, if you are going to make it public, chances are that your Firebase storage gets flooded by unwanted images from public. So, it is recommended to use an authentication mechanism to control this. This tutorial does not have authorization part.
Firebase comes with seemless integration with Angular framework for authentication part as well. Use the Sign-in Methods page under Authentication to enable and configure sign-in methods for your project. Here is the link for the same.
Please follow me if you liked this tutorial. Please give suggestions (if any) in the comments below. Show you support with your claps :). Cheers!
✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.