Slide 1

Slide 1 text

Supercharge your app Supercharge your app with Cloud Functions with Cloud Functions Photo by Johannes Plenio on Unsplash

Slide 2

Slide 2 text

Peter Friese Peter Friese Developer Advocate, Firebase Developer Advocate, Firebase @pete rf riese @pete rf riese

Slide 3

Slide 3 text

Information Overload Information Overload

Slide 4

Slide 4 text

Isn’t there an app for that?

Slide 5

Slide 5 text

Not invented here

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Codename Sofia ✨ Add links via iOS Share Extension ✨ Readability ✨ Extract OpenGraph Metadata ✨ Summarise (via GPT-3)

Slide 12

Slide 12 text

client-side backend

Slide 13

Slide 13 text

Firebase Backend Cloud Storage Cloud Firestore Serverless Architecture Authentication Client A miracle happens here ?

Slide 14

Slide 14 text

Firebase Backend Cloud Storage Cloud Firestore Serverless Architecture Authentication Client ? Custom Backend Node.js

Slide 15

Slide 15 text

Firebase Backend Cloud Storage Cloud Firestore Serverless Architecture Authentication Client ?

Slide 16

Slide 16 text

Firebase Backend Cloud Storage Cloud Firestore Serverless Architecture Authentication Client Cloud Functions

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Serverless JavaScript / TypeScript Automatic scaling Run in trusted environment

Slide 20

Slide 20 text

Di ff erent ways to call your functions

Slide 21

Slide 21 text

Di ff erent ways to call your functions Listen for HTTP requests Call a function from your app with extra context HTTPS Callable

Slide 22

Slide 22 text

Di ff erent ways to call your functions Using crontab or AppEngine cron.yaml syntax For resource-intensive, long- running tasks Scheduled In a queue

Slide 23

Slide 23 text

Di ff erent ways to call your functions Data wri tt en New user created User signing in Image uploaded Crashlytics ale r Analytics Conversions File deleted Data deleted Test run completed Data updated New con fi guration Con fi guration rollback React to events

Slide 24

Slide 24 text

Cloud Firestore Triggers onCreate(change, context) onUpdate(change, context) onDelete(change, context) onWrite(change, context) Execute a Cloud Function for the following events: const uid = context.auth.uid; const name = context.auth.token.name || null; const picture = context.auth.token.picture || null; const email = context.auth.token.email || null;

Slide 25

Slide 25 text

Store readable HTML for link

Slide 26

Slide 26 text

Store readable HTML for link Firebase Backend Cloud Firestore doc( ... ).onCreate() Server Web Site Client Cloud Functions

Slide 27

Slide 27 text

export const storeReadableLink = functions.firestore .document("artifacts/{documentId}") .onCreate(async (documentSnapshot) => { const url = documentSnapshot.data().url; const metadata = await parser.parse(url); const artifactDocument = documentSnapshot.data() as ArtifactDocument; if (!artifactDocument.readableHTML && metadata.content) { artifactDocument.readableHTML = metadata.content; } // update the document in Firestore return documentSnapshot.ref.update(artifactDocument); }); Store readable HTML for link

Slide 28

Slide 28 text

Callable Functions onCall(data, context) Call a Cloud Function directly from your app const uid = context.auth.uid; const name = context.auth.token.name || null; const picture = context.auth.token.picture || null; const email = context.auth.token.email || null; onRequest(request, response)

Slide 29

Slide 29 text

Summarise a rt icle

Slide 30

Slide 30 text

Summarise a rt icle Firebase Backend Cloud Firestore Server GPT-3 Client Cloud Functions https.onCall()

Slide 31

Slide 31 text

export const summarise = functions.https .onCall(async (data) => { const url = data; const completion = await openai.createCompletion({ model: "text-davinci-001", prompt: `Summarize this article: ${url}`, }); const summary = completion.data.choices[0].text; return summary; }); Summarise a rt icle

Slide 32

Slide 32 text

Can we make it reusable?

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Pre-built packages of functionality Based on Cloud Functions One-click install to Firebase project Con fi gurable

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

One more thing… One more thing…

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Building an Extension Install Firebase tools (CLI) Write Cloud Functions (triggers) Package Extension

Slide 41

Slide 41 text

name: auth-chat version: 0.2.0 specVersion: v1beta displayName: Authenticate with Stream Chat description: Synchronizes Firebase Authentication users with Stream, and creates and revoke Stream Chat authentication tokens. license: Apache-2.0 author: authorName: Stream url: https: / / getstream.io/ sourceUrl: https: // github.com/GetStream/stream-firebase-extensions/tree/ main/auth-chat billingRequired: true params: - param: LOCATION label: Cloud Functions location description: >- Where do you want to deploy the functions created for this extension? type: select extension.yaml

Slide 42

Slide 42 text

import * as admin from "firebase-admin"; import * as functions from "firebase-functions"; import { StreamChat } from "stream-chat"; admin.initializeApp(); const serverClient = StreamChat.getInstance( process.env.STREAM_API_KEY!, process.env.STREAM_API_SECRET!, ); // When a user is created in Firebase an associated Stream account is also created. export const createStreamUser = functions.auth.user().onCreate(async (user) => { functions.logger.log("Firebase user created", user); / / Create user using the serverClient. const response = await serverClient.upsertUser({ id: user.uid, name: user.displayName, email: user.email, image: user.photoURL, }); index.ts

Slide 43

Slide 43 text

import * as functions from "firebase-functions"; import { StreamChat } from "stream-chat"; admin.initializeApp(); const serverClient = StreamChat.getInstance( process.env.STREAM_API_KEY!, process.env.STREAM_API_SECRET!, ); // When a user is created in Firebase an associated Stream account is also created. export const createStreamUser = functions.auth.user().onCreate(async (user) => { functions.logger.log("Firebase user created", user); / / Create user using the serverClient. const response = await serverClient.upsertUser({ id: user.uid, name: user.displayName, email: user.email, image: user.photoURL, }); functions.logger.log("Stream user created", response); return response; }); index.ts

Slide 44

Slide 44 text

What’s new in Cloud Functions? Fewer cold sta rt s Concurrency Codebases Skip no-op deploys 2nd gen runtime Faster deployments Parameterized con fi gs Type-safe con fi gurations Suppo rt for Cloud Secret Manager Try them now Use alongside v1 functions Emulator suppo r

Slide 45

Slide 45 text

@pete rf riese @pete rf riese Thanks Thanks Thanks

Slide 46

Slide 46 text

Q&A Q&A Q&A