Generate production www web app folder from the Ionic app
Use following command in existing ionic project to generate production web app www folder.
ionic capacitor build browser --prod
This will generate a www folder, which we need to publish to the Firebase hosting.
Prepare Firebase Hosting project to publish the web app
Install Firebase CLI
Install Firebase CLI globally, if not already installed.
npm install -g firebase-tools
Login to Firebase, through Firebase CLI
Login to Firebase through firebase CLI, using following command. This will open a login screen in browser window. Login there.
firebase login
Create Firebase hosting project
Create new directory for firebase hosting project
mkdir hosting
Switch to directory
cd hosting
Initialize Firebase hosting project
Enter following to initialize firebase hosting project.
firebase init
Choose Yes, to initialize the Firebase project
Choose following hosting feature from listed options:
Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys
Next selected Use an existing project, because in my case I had an existing project.
Then select your project from the list.
Next question is
What do you want to use as your public directory? (public)
type www
Next Question
Configure as a single-page app (rewrite all urls to /index.html)? (y/N)
Choose Y
Next question
Set up automatic builds and deploys with GitHub? No
Enter, and firebase initialization will complete.
In the hosting folder one www folder will be created.
Copy www folder to Firebase hosting project
In the hosting folder one www folder will be created.
Replace this folder with the www folder generated above from Ionic production web app build .
Publish the web app
Finally, enter following command to publish the web app.
firebase deploy
Web app will be published to firebase hosting and you will be presented with the published web app url.
Congrats! We successfully published web app from ionic project to the Firebase hosting.
Conclusion
In this post we learned how to publish an Ionic web project to Firebase hosting.
First we generated the www folder from the Ionic app. Then We created and prepared a new project for publishing to Firebase hosting. In the Firebase hosting project we copied the www folder from the Ionic app and then finally published to Firebase hosting using firebase deploy
Firebase CLI provides a cloud function emulator which can be used to run the Firebase Cloud Function locally before deploying to production. Following are the types of functions which can be emulated.
HTTP functions
Callable functions
Background functions triggered from Authentication, Realtime Database, Cloud Firestore, and Pub/Sub.
We will test a simple http function which we created in the last post.
Firebase emulator is included in Firebase CLI, so we need to install it, or update it to the latest version.
npm install -g firebase-tools
Setting Up Admin Credentials for Emulated Functions
If your cloud function requires interaction with Google API or Firebase API via the Firebase Admin SDK then you may need to setup the admin credentials.
The Firebase emulator, which is included in the Firebase CLI, let’s us test the function locally before deploying to the cloud.
In this post we started with installing Firebase CLI and then we setup the Google Account Credential. Google Account Credential requires a JSON file with private key, which was generated in the Google Cloud Platform. Then we started the Firebase emulator and browsed the local function URL and received response from the local function.
Hope you liked this, please share your feedback or any query.
Firebase cloud functions let’s you run a piece of code in cloud without managing the servers. This is quite helpful when you just want to manage your code and not to worry about the servers executing the code. This pattern is also known as serverless architecture.
These cloud functions can be triggered by multiple events such as an http request, scheduled time or in response to changes in Realtime Database and perform the intended job.
In this post we will see how we can create a simple Firebase cloud function using Firebase CLI and then deploy it to Firebase. We will use TypeScript to write the function.
Let’s create and deploy a Firebase cloud function.
Installing Firebase CLI
Firebase cloud functions are created and deployed using Firebase CLI, so let’s install the Firebase CLI globally. Type following command on command prompt.
npm install -g firebase-tools
Login to Firebase CLI
We need to authenticate to Firebase to create and deploy cloud functions, authenticate to Firebase using following command.
firebase login
This will open a browser window to authenticate.
If you are logged in with another account then you can logout first using following.
firebase logout
Choose the account to login.
Allow, to grant permissions to Firebase CLI.
On Allow following success screen will be presented.
And in command prompt message like following will be logged.
Creating a Firebase Cloud Function Project
Create a directory for the project.
mkdir firebase-function-demo
Change to project directory.
cd firebase-function-demo
Open the directory with Visual Studio code or any other editor.
code .
Initialize Firebase functions project
firebase init functions
Accept the confirmation.
Choose the appropriate option for you. In my case I chose “Use an existing project“, because I have already created the Firebase project.
Next I chose the project from the presented list.
For this example we are going to use the TypeScript, so choose the TypeScript.
Choose Y if you want to use ESLint.
Select Y to install the dependencies.
Your project structure should appear like this so far.
Creating a Cloud Function
We will use the sample helloworld cloud function created by the Firebase CLI for this example.
Open selected index.ts.
index.ts contains commented sample function.
Uncomment the code, and save the file.
File contains one sample http request based cloud function. Which will log following.
"Hello logs!", {structuredData: true}
and return following response.
"Hello from Firebase!"
In the same file more functions can be added, for example we can add another scheduled function like following.
export const scheduledJob = functions.pubsub.schedule("every 5 minutes")
.onRun(()=>{
console.log("This will be run every 5 minutes.");
return null;
});
This scheduled function will run every five minutes,
cron expression can be added like following to define trigger time for scheduled functions.
export const scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
console.log('This will be run every day at 11:05 AM Eastern!');
return null;
});
Deploying cloud function
Let’s deploy the sample code in index.ts to cloud.
Execute the following command on command prompt.
firebase deploy
On successful function deployment, function url will be provided.
Paste the URL in the browser.
and you will get the response like below from cloud function.
Great! Our cloud function is successfully deployed and responding to http request.
Navigate to Firebase project and select functions.
and you should be able to see your cloud function.
You can switch to Logs tab to see the logs of the cloud function.
Congratulations! We have just deployed our first simple cloud function.
Conclusion
In this post we learned how we can create a Firebase Cloud Function and deploy to Firebase. We started with how to install the Firebase CLI globally, and how to authenticate to it. Then we created a template project for cloud function using Firebase CLI and then deployed and tested the cloud function.
In this blog post we will learn how to create a simple Notes application using Ionic 5 and Angular 12.
The Note application will be able to perform the read, create, update and delete operations, and Firestore database in the Firebase will be used as database.
For our Notes app we are going to save notes in the Firebase, and for that we need to create a new Firebase project. If you have existing Firebase project you can also use that.
Creating a Firebase project
To create a Firebase project head over to Firebase consoleand click on Add Project.
Firebase Console
Give your project a name, it’s Notes in my case.
You can change your project’s unique name here, although that doesn’t matters.
Project name
Enable Google Analytics to your project (optionally).
Enable Google Analytics
If you have chosen to enable Google Analytics then select or create the account for the Google Analytics.
Account for Google Analytics
In the following I have selected a new Google Analytics account.
New Google Analytics account
Then finally click on create project.
This will take a few minutes to setup the new Firebase project.
Setting up Firebase project.Firebase project setup done.
Click on continue.
and now we are inside the our brand new Firebase project.
Firebase project overview
Getting Firestore Database settings.
To store the Notes inside Firebase, we need some configuration settings from the Firebase project.
To get the settings, go to the Project settings.
Project settings option
Select the web app options from available platform options.
Web app option
Click on Register app.
App name
Copy the firebaseConfig from the next screen. We are going to need this in our Ionic app.
Then click on Continue to console at the bottom of the screen.
In the firebaseConfig one setting is missing in my case i.e. databaseURL.
The databaseURL specifies to which database data will be saved.
If this is missing in your case too, then add like following.
In this post we learned how to perform the CRUD operations in Firestore database inside Firebase using Ionic 5 application which was created using Angular 12.
We started with setting up the Firebase project to get firestore database settings. Then we created the ionic 5 application using ionic CLI and then added support for all of the Read, Create, Update and Delete operations.
Please leave your feedback or comment if you are facing any issue.