Slide 1

Slide 1 text

Build and scale serverless mobile apps with Firebase and Google Cloud Doug Stevenson @CodingDoug

Slide 2

Slide 2 text

Doug Stevenson Language is hard!

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Doug Stevenson

Slide 6

Slide 6 text

Hello! Bonjour!

Slide 7

Slide 7 text

API Service App Server

Slide 8

Slide 8 text

API Service App

Slide 9

Slide 9 text

Build better apps Auth Cloud Functions Cloud Firestore Hosting ML Kit Realtime Database Cloud Storage Improve app quality Crashlytics Performance Monitoring Test Lab Grow your app Analytics Predictions Cloud Messaging Remote Config A/B Testing Dynamic Links In-app Messaging bit.ly/what-is-firebase

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API Translation API Hello! Bonjour!

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Firebase Authentication ● Every user gets a unique ID ● Restrict who can read and write what data ● Give each user a personal space to work with ● Create your own audit trails

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Firebase Authentication login startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders(listOf( AuthUI.IdpConfig.GoogleBuilder().build(), AuthUI.IdpConfig.EmailBuilder().build() )) .build(), RC_SIGN_IN)

Slide 16

Slide 16 text

Firebase Authentication login override fun onActivityResult(reqCode: Int, resCode: Int, data: Intent) { super.onActivityResult(reqCode, resCode, data) if (reqCode == RC_SIGN_IN) { if (resCode == Activity.RESULT_OK) { // Login success user = auth.currentUser } else { // Login error } } }

Slide 17

Slide 17 text

App

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Cloud Firestore ● Cloud-hosted ● NoSQL ● Realtime ● Mobile access controlled with security rules

Slide 20

Slide 20 text

Uploads upload1 - language: "en" - url: "gs://bucket/upload1" upload2 - language: "es" - url: "gs://bucket/upload2" upload3 - language: "ja" - url: "gs://bucket/upload3" Collection Document Fields

Slide 21

Slide 21 text

Add speech metadata to Firestore val recording = Recording() recording.contentType = "audio/amr" recording.encoding = "AMR" recording.sampleRate = 8000 recording.language = "en"

Slide 22

Slide 22 text

Add speech metadata to Firestore val recording = Recording() recording.contentType = "audio/amr" recording.encoding = "AMR" recording.sampleRate = 8000 recording.language = "en" val firestore = FirebaseFirestore.getInstance() val docRef = firestore.collection("uploads").document()

Slide 23

Slide 23 text

Add speech metadata to Firestore val recording = Recording() recording.contentType = "audio/amr" recording.encoding = "AMR" recording.sampleRate = 8000 recording.language = "en" val firestore = FirebaseFirestore.getInstance() val docRef = firestore.collection("uploads").document() docRef.set(recording) .addOnSuccessListener { ... } .addOnFailureListener { e -> ...}

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

App Cloud Firestore

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Cloud Storage for Firebase ● Uploads and downloads direct from app ● Robust ● Secure ● Mobile access controlled with security rules

Slide 28

Slide 28 text

Upload speech file to Cloud Storage val storage = FirebaseStorage.getInstance() val storageRef = storage.reference .child("uploads") .child(user!!.uid) .child(docRef.id)

Slide 29

Slide 29 text

Upload speech file to Cloud Storage val storage = FirebaseStorage.getInstance() val storageRef = storage.reference .child("uploads") .child(user!!.uid) .child(docRef.id) storageRef.putFile(Uri.fromFile(recordingFile)) .addOnSuccessListener { ... } .addOnFailureListener { ... }

Slide 30

Slide 30 text

Cloud Storage App Cloud Firestore

Slide 31

Slide 31 text

Cloud Storage Cloud Firestore Transcribe & Translate

Slide 32

Slide 32 text

Cloud Speech API Cloud Translation API

Slide 33

Slide 33 text

Cloud Storage App Cloud Firestore Cloud Speech API Translation API

Slide 34

Slide 34 text

API Service App Server

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Cloud Functions ● Deploy code that responds to events ● Fully managed node.js environment ● Pay only for what you use ● Secure

Slide 37

Slide 37 text

Cloud Functions Triggers ● HTTP endpoint ● Google Cloud Storage ● Pub/sub topic message ● Cloud Firestore

Slide 38

Slide 38 text

Additional Firebase Triggers ● Realtime Database ● Authentication ● Google Analytics for Firebase ● Crashlytics ● Remote Config ● Test Lab ● Scheduled (cron)

Slide 39

Slide 39 text

Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API Translation API

Slide 40

Slide 40 text

Cloud Functions Storage trigger export const onRecordingUpload = functions.storage.object().onFinalize(async (object, context) => { // Plenty of code here that performs the translations! })

Slide 41

Slide 41 text

Handle input const path = object.name // path: uploads/{uid}/{docId} const parts = path.split('/') if (parts.length !== 3) { console.log("Path isn't three parts long") return } const docId = parts[2]

Slide 42

Slide 42 text

Read speech metadata from Firestore const docRef = firestore.collection('uploads').doc(docId) const snapshot = await docRef.get() const docData = snapshot.data()

Slide 43

Slide 43 text

Read speech metadata from Firestore const docRef = firestore.collection('uploads').doc(docId) const snapshot = await docRef.get() const docData = snapshot.data() const languageCode = docData.language const sampleRateHertz = docData.sampleRate const encoding = docData.encoding

Slide 44

Slide 44 text

Call Speech and Translate APIs const recognizeRequest = { config: { languageCode, sampleRateHertz, encoding }, audio: { uri : `gs://${bucket.name}${docData.storagePath}`} }

Slide 45

Slide 45 text

Call Speech and Translate APIs const recognizeRequest = { config: { languageCode, sampleRateHertz, encoding }, audio: { uri : `gs://${bucket.name}${docData.storagePath}`} } const recognizeResponse = await speechClient.recognize(recognizeRequest) const transcript = recognizeResponse[0].results[0].alternatives[0].transcript

Slide 46

Slide 46 text

Call Speech and Translate APIs const recognizeRequest = { config: { languageCode, sampleRateHertz, encoding }, audio: { uri : `gs://${bucket.name}${docData.storagePath}`} } const recognizeResponse = await speechClient.recognize(recognizeRequest) const transcript = recognizeResponse[0].results[0].alternatives[0].transcript // Foreach language to translate: translate.translate(transcript, { from: fromLanguage, to: toLanguage })

Slide 47

Slide 47 text

Call Speech and Translate APIs // Assemble final translations object // const translations = { // "ja": "こんにちは世界", // "es": "Hola Mundo", // "nl": "Hallo Wereld" // } await docRef.update({ translations })

Slide 48

Slide 48 text

Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API Translation API

Slide 49

Slide 49 text

Translation API

Slide 50

Slide 50 text

Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API Translation API

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Query for latest speech document const uploads = firebase.firestore().collection('uploads') uploads .where('timeCreated', '>', new Date()) .orderBy('timeCreated', 'desc') .limit(1) .get() .then(snapshot => ...)

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Query for latest speech document const uploads = firebase.firestore().collection('uploads') uploads .where('timeCreated', '>', new Date()) .orderBy('timeCreated', 'desc') .limit(1) .get() .then(snapshot => ...)

Slide 55

Slide 55 text

Receive realtime updates to results const uploads = firebase.firestore().collection('uploads') const unsubscribe = uploads .where('timeCreated', '>', new Date()) .orderBy('timeCreated', 'desc') .limit(1) // .get() .onSnapshot(onNext) function onNext(querySnapshot) { // querySnapshot contains updated search results }

Slide 56

Slide 56 text

Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API Translation API Hello! Bonjour!

Slide 57

Slide 57 text

How do you scale it? You do nothing.

Slide 58

Slide 58 text

Cloud Firestore Scaling ● Massively scalable ● Queries scale with the size of the result set ○ ~ 50 in 1,000 ○ ~ 50 in 100,000 ○ ~ 50 in 100,000,000 ● Firestore will not perform an unscalable query

Slide 59

Slide 59 text

Cloud Storage Scaling ● Exabyte scale ○ 1 with 18 zeros after it (1x1018) ○ 1 million terabytes ○ Trillions of high-res photos

Slide 60

Slide 60 text

Cloud Functions Scaling ● Auto scales up and down ● Up to 1000 concurrent server instances for each background trigger ● Bandwidth- and rate-limited scaling for HTTP triggers

Slide 61

Slide 61 text

Thank you! Doug Stevenson @CodingDoug github.com/CodingDoug/universal-translator Get the code: