Build and scale serverless mobile apps with Firebase and Google Cloud

Build and scale serverless mobile apps with Firebase and Google Cloud

If your app has millions of users, you need a backend infrastructure to support its growth. Do you want to build that infrastructure? Of course not! Your time is better spent focusing on the app. In this talk, we'll take a look at the code for a mobile app that works like a “universal translator”. It uses Firebase and Google Cloud services and APIs. And we'll do it "serverless" without having to manage servers or infrastructure.

3acd4fb373289e71fd7ebfb287a75a3b?s=128

Doug Stevenson

June 06, 2019
Tweet

Transcript

  1. Build and scale serverless mobile apps with Firebase and Google

    Cloud Doug Stevenson @CodingDoug
  2. Doug Stevenson Language is hard!

  3. None
  4. None
  5. Doug Stevenson

  6. Hello! Bonjour!

  7. API Service App Server

  8. API Service App

  9. 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
  10. None
  11. Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API

    Translation API Hello! Bonjour!
  12. None
  13. 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
  14. None
  15. Firebase Authentication login startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders(listOf( AuthUI.IdpConfig.GoogleBuilder().build(), AuthUI.IdpConfig.EmailBuilder().build() ))

    .build(), RC_SIGN_IN)
  16. 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 } } }
  17. App

  18. None
  19. Cloud Firestore • Cloud-hosted • NoSQL • Realtime • Mobile

    access controlled with security rules
  20. 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
  21. Add speech metadata to Firestore val recording = Recording() recording.contentType

    = "audio/amr" recording.encoding = "AMR" recording.sampleRate = 8000 recording.language = "en"
  22. 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()
  23. 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 -> ...}
  24. None
  25. App Cloud Firestore

  26. None
  27. Cloud Storage for Firebase • Uploads and downloads direct from

    app • Robust • Secure • Mobile access controlled with security rules
  28. Upload speech file to Cloud Storage val storage = FirebaseStorage.getInstance()

    val storageRef = storage.reference .child("uploads") .child(user!!.uid) .child(docRef.id)
  29. 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 { ... }
  30. Cloud Storage App Cloud Firestore

  31. Cloud Storage Cloud Firestore Transcribe & Translate

  32. Cloud Speech API Cloud Translation API

  33. Cloud Storage App Cloud Firestore Cloud Speech API Translation API

  34. API Service App Server

  35. None
  36. Cloud Functions • Deploy code that responds to events •

    Fully managed node.js environment • Pay only for what you use • Secure
  37. Cloud Functions Triggers • HTTP endpoint • Google Cloud Storage

    • Pub/sub topic message • Cloud Firestore
  38. Additional Firebase Triggers • Realtime Database • Authentication • Google

    Analytics for Firebase • Crashlytics • Remote Config • Test Lab • Scheduled (cron)
  39. Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API

    Translation API
  40. Cloud Functions Storage trigger export const onRecordingUpload = functions.storage.object().onFinalize(async (object,

    context) => { // Plenty of code here that performs the translations! })
  41. 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]
  42. Read speech metadata from Firestore const docRef = firestore.collection('uploads').doc(docId) const

    snapshot = await docRef.get() const docData = snapshot.data()
  43. 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
  44. Call Speech and Translate APIs const recognizeRequest = { config:

    { languageCode, sampleRateHertz, encoding }, audio: { uri : `gs://${bucket.name}${docData.storagePath}`} }
  45. 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
  46. 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 })
  47. Call Speech and Translate APIs // Assemble final translations object

    // const translations = { // "ja": "こんにちは世界", // "es": "Hola Mundo", // "nl": "Hallo Wereld" // } await docRef.update({ translations })
  48. Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API

    Translation API
  49. Translation API

  50. Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API

    Translation API
  51. None
  52. Query for latest speech document const uploads = firebase.firestore().collection('uploads') uploads

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

    .where('timeCreated', '>', new Date()) .orderBy('timeCreated', 'desc') .limit(1) .get() .then(snapshot => ...)
  55. 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 }
  56. Cloud Storage App Cloud Functions Cloud Firestore Cloud Speech API

    Translation API Hello! Bonjour!
  57. How do you scale it? You do nothing.

  58. 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
  59. Cloud Storage Scaling • Exabyte scale ◦ 1 with 18

    zeros after it (1x1018) ◦ 1 million terabytes ◦ Trillions of high-res photos
  60. 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
  61. Thank you! Doug Stevenson @CodingDoug github.com/CodingDoug/universal-translator Get the code: