Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Firebase Cookbook
Firebase Cookbook

Firebase Cookbook: Over 70 recipes to help you create real-time web and mobile applications with Firebase

By Houssem Yahiaoui
$15.99 per month
Book Nov 2017 288 pages 1st Edition
eBook
$35.99 $24.99
Print
$43.99
Subscription
$15.99 Monthly
eBook
$35.99 $24.99
Print
$43.99
Subscription
$15.99 Monthly

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Nov 29, 2017
Length 288 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781788296335
Vendor :
Google
Category :
Table of content icon View table of contents Preview book icon Preview Book

Firebase Cookbook

Firebase - Getting Started

In this chapter, we will cover the following recipes:

  • Creating your first Firebase app
  • Adding Firebase to an existing frontend project
  • Integrating Firebase in the backend
  • Integrating Firebase in Android applications
  • Integrating Firebase in iOS applications

Introduction 

With the changing web and mobile scene, finding a suitable solution and technology that suits these climbing, fast needs is a must. From web development to mobile, the way we treat APIs, data, and security, and how we make users feel as engaged as we possibly can, is an urgent matter.

Going from a traditional setup to the cloud leads to many new patterns and architectures. Backend-as-a-service (BaaS), saves us from tons of useless setup and configuration and makes us think of nothing but our application logic.

Here we are introducing Firebase, a feature-heavy BaaS that will make creating your next awesome project a breeze. It will eliminate many tedious tasks and even manpower and will create your server-side code and give you a more secure, well-built platform that will completely change your thoughts about simplicity and scalability.

So, hold on tight and let's start our journey together by creating our first ever Firebase application.

Creating your first Firebase application

The process of creating a Firebase application is straightforward and mainly visual. This recipe will demonstrate the process of creating a Firebase application from scratch.

How to do it...

  1. As mentioned previously, we will use nothing but our clicking superpower and our favorite browser. First, head directly to the Firebase official website: https://firebase.google.com/. A screenshot of the website page is as follows (Figure 1):
Figure 1: Firebase main screen
  1. Now, go to the top navigation bar and click on the SIGN IN button. This will lead to another page where you will be introduced to the Google authentication page showcased in the following figure, where you can select the most suitable account for your development work (Figure 2):
Figure 2: Firebase - Google authentication
  1. Now, after you select the most suitable Google account, you will find yourself redirected to this link: https://console.firebase.google.com/. You will find all of your Firebase projects here, and you can introduce new projects as well (Figure 3):
Figure 3: Firebase console

Now, we do have two options: whether to import a Google project or simply start a fresh one. Let us learn how to start a new project.

  1. After clicking on the Add project plus button, you will be introduced to a model where you can add the Project name and Country/region. Keep in mind that the Project name and Country are variables, so you can change their values to the values that suit you best. You can see the page for creating the project in the following screenshot (Figure 4):
Figure 4: Firebase project creation
  1. After finishing the previous step, you will be redirected to your Firebase dashboard (Figure 5):
Figure 5: Firebase project dashboard

Congratulations! You've successfully created your first Firebase project. You have seen that the steps are really simple and are applicable to any Firebase project that you may create now or in the future.

Adding Firebase to an existing frontend project

Since Firebase is indeed a backend platform which typically acts as a service, it's not strange to see today's developer ditch the idea of creating a backend in general. They just focus on their frontend, which is actually the main idea behind serverless architecture nowadays.

How to do it...

In order to fully integrate Firebase into our frontend project, which is typically composed of nothing but .html, .css, and .js files, we will need to follow the given steps:

  1. Open your favorite code editor and write down the following:
      <script 
src="https://www.gstatic.com/firebasejs/3.9.0/firebase.js>
</script>
<script>
// Initialize Firebase
// TODO: Replace with your project's customized
code snippet
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
};
firebase.initializeApp(config);
</script>

So, what we've just done is simply imported the Firebase core library from its CDN and initialized it with a configuration object that Firebase gave us out of the box.

  1. Now, let's grab our pre-filled configuration form our Firebase project dashboard. The steps are actually easy--login to your Firebase project. (Figure 6):
Figure 6: Firebase Application Overview/Management section.
  1. Now, simply click on the magenta-colored button--Add Firebase to your web app--and a new model will appear holding all the required metadata (Figure 7):
Figure 7: Firebase project credentials
  1. Now simply copy and paste the code snippet on the screen into your index.html page and you're good to go.

Congratulations, you've successfully integrated Firebase within your Firebase project. Do keep in mind that Firebase services are very modular, so you won't have that heavyweight large dependency that you could simply exploit one--or at most four--resources from.

For the next module, we will see how we can integrate Firebase with our backend application.

How it works...

In the previous steps, we integrated the Firebase JavaScript client over our web page, and we also created the basic backbone configuration. Following the documentation guidelines, we copied/pasted the configuration script that would hold all the required tokens and API keys that Firebase was going to need in order to support our functionalities.

Integrating Firebase into the backend

Firebase is already a complete solution that can simply replace our backend, but sometimes, due to some requirements, you will find yourself integrating Firebase into the already present backend.

In this integration process, we will integrate Firebase services in a NodeJS backend application.

How to do it...

Since we're using NodeJS, integrating Firebase is one module setup away:

  1. Head directly to your terminal (cmd on Windows) and write down the following command:
     ~ cd project-directory
~/project-directory ~> npm install firebase --save

Now, the preceding command will go and download Firebase locally so you can access it directly using your normal commonJS workflow.

  1. Now, you will need to grab yourself a copy of your Firebase project configuration. This step is relatively easy as you can find the required configuration metadata by following the steps mentioned in the previous section, Adding Firebase to an existing frontend project, where we introduced how we can add Firebase to a frontend project.
  2. Head directly to your favorite code editor and write down the following:
      // [*] 1: requiring/importing Firebase in our 
workflow
const firebase = require('firebase');

// [*] 2:Initialising our application with our
credentials
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL:
"https://<DATABASE_NAME>.firebaseio.com",
storageBucket: "<BUCKET>.appspot.com",
};
firebase.initializeApp(config);

Congratulations, you've successfully integrated Firebase within your backend workflow. I also want to point out that we can extend the workflow we have now using something called Firebase Admin SDK. We will cover how we integrate it and work with it in Chapter 7, Firebase Admin SDK.

How it works...

Similar to the frontend integration, in our backend we are doing the following:

  1. Using a node package manager or npm to install the Firebase commonJS library, where we will find all the necessary APIs.
  2. We're requiring/importing Firebase to be part our application, passing it to the configuration object that will hold all our API keys, links, and more.
  3. Lastly, we're initializing our application with the configuration object we just created.

Integrating Firebase in Android applications

With Android Studio 2.0 and up, the Android Studio IDE becomes more Firebase friendly and the process of integrating Firebase's different components is nothing but a pleasing experience.

Getting ready 

In order to create a Firebase Android-ready application, you need to make sure that Android Studio is present on your development machine. You can download the suitable version that suits your development machine operating system at https://developer.android.com/studio/index.html.

How to do it...

After successfully downloading the Android Studio, launch it and you will be greeted with the following screen (Figure 8):

Figure 8: Android Studio welcome screen

Now, let's create a new application. The process is straightforward, and is as follows:

  1. When you fill in the application name, application type, and suitable used SDK, your Android application development workflow will look something like this (Figure 9):
Figure 9: Android Studio after application launch
  1. Now the fun can begin. Head directly to the Android Studio menu bar. Click on the Tools menu option and you will see a menu item that includes many options including Firebase (Figure 10):
Figure 10: Integrating Firebase in our Android application
  1. After clicking on it, go to the right side, to the Assistant section. There you will find the Firebase section, with all the goods that it can offer (Figure 11):
Figure 11: Android Firebase integration - part one

In this section, you will see all that Firebase has to offer--there are different sections and areas as we talked about previously. Firebase is a set of services, which means that each part is a service in itself. This also means that you're free to select whatever service you want to include. For the sake of this chapter, we're taking the Realtime Database as the option to finalize the integration process.

The integration process used in this example is applicable to all sorts of Firebase services in the exact same way.
  1. After clicking on the Realtime Database option you will get a submenu with a simple explanation or description as to what the service actually does (Figure 12):
Figure 12: Android Firebase integration - part two
  1. Now, simply click on the Save and retrieve data link option and you will start a new process that will combine both authenticating as well as download and install the Firebase component in your application (Figure 13):
Figure 13: Firebase project integration - part three

You will need to configure your project by following the previous instructions. Next, you will need to authenticate and use the Gmail account related to your Firebase project.

  1. Once you have clicked on the link, you will need to select the Google account linked to your project. In doing so, you will need to authorize Android Studio to use your Google account. Then, whenever you approve those authorization rules, you will be redirected to the following page (Figure 14):
Figure 14: Firebase project integration - part four

Congratulations! Android Studio is now fully connected to your Google account. By now, you will see a new model popping up in your Android Studio. As mentioned previously, you will need to either select or create a new Firebase project. In our case, we have already created our awesome project, so we will only need to select it and hit the Connect to Firebase button (Figure 15).

Figure 15: Firebase project integration - part five

Now, Android Studio will take some seconds to connect to your project and configure your awesome application. Then you will get the following lovely green button from heaven, indicating that everything went smoothly (Figure 16).

Figure 16: Firebase project integration - part six

Congratulations! Your application is now fully connected and well prepared to host your Firebase logic; all you need to do next is to integrate the service you want the most and simply work with it. We will see all that and more starting from Chapter 11, Integrating Firebase with Android/iOS Natively.

Integrating Firebase in iOS applications

Integrating Firebase our iOS application involves adding the Firebase package similar to any other package you have probably worked with in the past.

Getting ready 

In order to create and integrate Firebase within your application, you will need to have a MacBook Pro or one of Apple's computer variants so that you can follow the upcoming steps; you will also need to install Xcode.

How to do it...

In order to create an iOS application, open Xcode and follow the given steps:

  1. Create a new project or open your already created project (Figure 17):
Figure 17: Xcode project opening/creation
  1. In our case, we're about to start a new project called firebasecookbook. It's going to be based on the Xcode single-view application project template.
In our application--or when it comes to an example provided in this book regarding Firebase and iOS--we will work with Swift instead. It's just a personal preference, but you can use the one that suits you best.
Figure 18: Xcode project creation

Don't forget to copy that Bundle Identifier, because we will need it in the next step. In this case, our Bundle Identifier is hcodex.firebasecookbook.

  1. Go to your Firebase dashboard and click on the Add Firebase to your iOS app button. After clicking on it, you will get a configuration model with some steps that will guide you in your Firebase iOS integration (Figure 19).
Figure 19: Xcode project creation
  1. Remember that bundle id or Bundle Identifier? Copy and paste that ID in its designated place and add a nickname for your application if you wish. Click on the REGISTER APP button.
  2. Next, we will need to download a special plist file called the GoogleService-info.plist file. This file will have all the necessary metadata that will be included in your project (Figure 20).
Figure 20: Firebase GoogleService-info.plist download
  1. Now simply copy and paste that file into your project (Figure 21):
Figure 21: Firebase GoogleService-info.plist in our application.
  1. After we have finished the file download and file integration, let's just install some dependencies. In this process, we will use CocoaPods as our package manager. Head directly to your project using your terminal:
      ~ cd project-directory
~/project-directory ~> pod init
To download and install CocoaPods on your macOS development machine, please follow the steps mentioned on the official website: https://guides.cocoapods.org/using/getting-started.html.

After you initialize your project with CocoaPods, you will find a newly created file named Podfile.

The Podfile is a specification that describes the dependencies of the targets of one or more Xcode projects.

  1. Now, you will need to edit the Podfile using your favorite code or text editor and add the following line:
      pod 'Firebase/Core'
  1. Now, save the file, go back to your terminal, and write down the following command:
       ~/project-directory ~> pod install 

This command will download and install all the required libraries and modules that Firebase needs to fire up within your application.

  1. Now, instead of your regular project, open another special project extension, as the following command shows:
       ~/project-directory ~> open project-name.xcworkspace
  1. We're one step behind. Now, in your application, go directly to your AppDelegate and simply import Firebase using the following code snippet:
      import Firebase
  1. Now, in the didFinishLaunchingWithOptions method, add the following code:
      FIRApp.configure()

Congratulations! You've successfully integrated Firebase with your iOS application.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • A Solution based approach that would help you create high-quality apps for your businesses
  • Harness the power of real-time database to create apps that work on multiple platforms
  • Build a customized solution for your app development challenges with Firebase

Description

Do you feel tired just thinking or even hearing about backend technologies, authentication or the tedious task of deployment? Firebase is here to change the way you develop and make your app a first-class citizen of the cloud. This books takes a solution based approach by providing you recipes that would help you understand the features of Firebase and implement them in your existing web or mobile applications. We start-off by creating our first Firebase application and integrating its services into different platforms and environments for mobile as well as web applications. Then we deep dive into Real-time Database and Firebase Storage that allows your users to access data across various devices with realtive ease. With each chapter you will gradually create the building blocks of your application from securing your data with Firebase Rules to authenticating your users with O-Auth. Moving along we would explore modern application development techniques such as creating serverless applications with Firebase Cloud Functions or turning your traditional applications into progressive apps with Service workers. Finally you will learn how to create cross-platform mobile apps, integrate Firebase in native platforms, and learn how to monetize your mobile applications using Admob for Android and iOS.

What you will learn

Use Firebase Diverse Authentication systems Integrate easy, secure File Hosting using Firebase Storage services Make your application serverless using Firebase Cloud Functions Use the powerful Firebase Admin SDK for privilege management Use Firebase within NativeScript apps for cross-platform applications Modify, structure, save and serve data in and from Realtime Database Get acquainted with the newly introduce Cloud Firestore, a scalable database for your web and mobile applications

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Nov 29, 2017
Length 288 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781788296335
Vendor :
Google
Category :

Table of Contents

15 Chapters
Preface Chevron down icon Chevron up icon
1. Firebase - Getting Started Chevron down icon Chevron up icon
2. Firebase Real-Time Database Chevron down icon Chevron up icon
3. File Management with Firebase Storage Chevron down icon Chevron up icon
4. Firebase Authentication Chevron down icon Chevron up icon
5. Securing Application Flow with Firebase Rules Chevron down icon Chevron up icon
6. Progressive Applications Powered by Firebase Chevron down icon Chevron up icon
7. Firebase Admin SDK Chevron down icon Chevron up icon
8. Extend Firebase with Cloud Functions Chevron down icon Chevron up icon
9. We’re Done, Let’s Deploy Chevron down icon Chevron up icon
10. Integrating Firebase with NativeScript Chevron down icon Chevron up icon
11. Integrating Firebase with Android/iOS Natively Chevron down icon Chevron up icon
12. Hack Application's Growth Chevron down icon Chevron up icon
13. Adding Analytics and Maximizing Earnings Chevron down icon Chevron up icon
14. Firebase Cloud FireStore Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.