You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Text Translator With Firebase Cloud Functions onWrite and Angular
Episode 15 written by Jeff DelaneyIn this lesson, we are going to use Firebase Cloud Functions to run code in the background when new data is created in a specific part of the database, using the onWrite event handler. This will allow us to abstract CPU or memory intensive tasks outside of the frontend Angular app.
We are going to build simple text translator using the Google Translate API. The user can paste write something into a textarea, then click the translate button to trigger the function. Running multiple translations client-side would add way too much overhead to our app, so were going to delegate it to a cloud function.
Building the Translator App
The Angular app is going to use a service to push the user’s initial English text to the database. The component will subscribe to the data and update the translations in realtime when the Google Translate finishes its work.
ng g service translate |
The NoSQL database structure looks like this. User input must be in english, and translations are correspond to their language code (i.e. fr==French, ar==Arabic)
translations |
The Translate Service
Our service has one simple task - Create a new translation in the database and return it as a FirebaseObjectObservable
.
import { Injectable } from '@angular/core'; |
The Text Translate Component
Nothing too fancy here, just a textarea
to accept the user input, then a function that will push it to the database and observe the newly created object.
import { Component } from '@angular/core'; |
In the template, we display the translation or say “translation running the cloud” (this could also be replaced with a loading spinner).
<h1>Translate Your Text</h1> |
Firebase Cloud Function using onWrite Handler
In order to use Google Translate, you need to active the API from your GCP console.
If this is your first cloud function, run firebase init
, then select functions.
Go into the functions/
directory and run npm install
with the following packages.
{ |
The cloud function does all of the heavy lifting. This is a translation microservice - completely isolated from the Angular app. It has its own NodeJS environment and has zero impact on Angular’s frontend performance. It is loosely based on the official cloud function sample.
The function is trigged during the onWrite
event, which occurs when data is changed at the corresponding Firebase database reference point - in this case /translations/{translationId}
When triggered, it will take a snapshot of the data, then create a Promise
for each language we want to translate. Each promise represents a request to the Google Translate API. Google Translate expects a source
language (english), a target
language to translate to, and q
the text to translate.
After the response is received from Google Translate, the Firebase database is updated with the translated data for each language. The Firebase admin
package is used to overwrite any database rules scoped to the user.
functions/index.js
var functions = require('firebase-functions'); |
That’s it for Firebase cloud functions with onWrite
.