SlideShare a Scribd company logo
tinyurl.com/functions-lab | @bretmcg
Cloud Functions for Firebase
Bret McGowen
@bretmcg
tinyurl.com/functions-lab
tinyurl.com/functions-lab | @bretmcg
Contents
What are Cloud Functions for Firebase?
Why Cloud Functions?
Using Authentication Triggers
Using Storage Triggers
Using Database Triggers
tinyurl.com/functions-lab | @bretmcgtinyurl.com/functions-lab | @bretmcg
Automatically run backend code
in response to events triggered
by Firebase features
Cloud Functions
for Firebase
tinyurl.com/functions-lab | @bretmcg@bretmcg
Google's new serverless data center
not
^
@bretmcg
tinyurl.com/functions-lab | @bretmcg
Serverless
No need to even think about servers
No upfront provisioning; scales as needed
Pay per use
Stateless / ephemeral
tinyurl.com/functions-lab | @bretmcg
Cloud StorageAuthentication
Respond to Events
Realtime Database
tinyurl.com/functions-lab | @bretmcg
Why Cloud Functions?
Extends and connects
Firebase features
Low maintenance Keeps your logic
private and secure
tinyurl.com/functions-lab | @bretmcg
FriendlyChat
Realtime chat app
tinyurl.com/functions-lab | @bretmcg
Setting Up FriendlyChat
tinyurl.com/functions-lab
or
https://codelabs.developers.google.com/
and navigate to Cloud Functions Codelab
tinyurl.com/functions-lab | @bretmcg
Setting Up FriendlyChat
02.
Get the Sample Code
git clone
https://github.com/firebase/f
riendlychat
01.
Create a Firebase project and
Set up your app
From the console, create new
project called FriendlyChat and
enable Google Authentication
Install Firebase CLI and
deploy FriendlyChat
$ npm -g install firebase-tools
$ firebase deploy --except
functions
03.
tinyurl.com/functions-lab | @bretmcg
Setting Up FriendlyChat
02.
Get the Sample Code
git clone
https://github.com/firebase/f
riendlychat
01.
Create a Firebase project and
Set up your app
From the console, create new
project called FriendlyChat and
enable Google Authentication
Install Firebase CLI and
deploy FriendlyChat
$ npm -g install firebase-tools
$ firebase deploy --except
functions
03.
tinyurl.com/functions-lab | @bretmcg
Setting Up FriendlyChat
02.
Get the Sample Code
git clone
https://github.com/firebase/f
riendlychat
01.
Create a Firebase project and
Set up your app
From the console, create new
project called FriendlyChat and
enable Google Authentication
Install Firebase CLI and
deploy FriendlyChat
$ npm -g install firebase-tools
$ firebase deploy --except
functions
03.
tinyurl.com/functions-lab | @bretmcg
Try it Out!
Send some messages with
FriendlyChat
tinyurl.com/functions-lab | @bretmcg
Database Structure
messages: {
-K2ib4H77rj0LYewF7dP: {
name: "Jen",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg",
text: "lol that's awesome!"
},
-K2ib5JHRbbL0NrztUfO: {
imageUrl: "gs://friendlychat-833ed.appspot.com/GJHZV...D0A.JPG",
name: "Bret",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg"
},
...
}
tinyurl.com/functions-lab | @bretmcg
Database Structure
messages: {
-K2ib4H77rj0LYewF7dP: {
name: "Jen",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg",
text: "lol that's awesome!"
},
-K2ib5JHRbbL0NrztUfO: {
imageUrl: "gs://friendlychat-833ed.appspot.com/GJHZV...D0A.JPG",
name: "Bret",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg"
},
...
}
tinyurl.com/functions-lab | @bretmcg
Database Structure
messages: {
-K2ib4H77rj0LYewF7dP: {
name: "Jen",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg",
text: "lol that's awesome!"
},
-K2ib5JHRbbL0NrztUfO: {
imageUrl: "gs://friendlychat-833ed.appspot.com/GJHZV...D0A.JPG",
name: "Bret",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg"
},
...
}
tinyurl.com/functions-lab | @bretmcg
Database Structure
messages: {
-K2ib4H77rj0LYewF7dP: {
name: "Jen",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg",
text: "lol that's awesome!"
},
-K2ib5JHRbbL0NrztUfO: {
imageUrl: "gs://friendlychat-833ed.appspot.com/GJHZV...D0A.JPG",
name: "Bret",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg"
},
...
}
tinyurl.com/functions-lab | @bretmcg
Cloud Functions environment
- Node.js 6.11.1
- Open-source, cross-platform
JavaScript run-time environment for
executing JavaScript code server-side
- Non-blocking - commands execute in
parallel, and use callbacks to signal
completion or failure
- Debian 8 “Jessie”
- Env contains ImageMagick
tinyurl.com/functions-lab | @bretmcg
// callback-hell.js
doThing1(function(a){
doThing2(a, function(b){
doThing3(b, function(c){
doThing4(c, function(d){
doThing5(d, function(e){
// ...
});
});
});
});
});
tinyurl.com/functions-lab | @bretmcg
Promises
tinyurl.com/functions-lab | @bretmcg
return mkdirp(tmpDir).then(() => {
return file.download({destination: tmpFile});
}).then(() => {
console.log('File downloaded to ', tmpFile);
return spawn(...); //create the thumbnail
}).then(() => {
console.log('Thumbnail: ', tmpThumbFile);
return bucket.upload(tmpFile, {destination: thu});
}).catch(function(error) {
console.log("Argghhh!", error);
});
tinyurl.com/functions-lab | @bretmcg
Authentication
Triggers
Trigger a function to run when
a new user is created/deleted
Welcome!
tinyurl.com/functions-lab | @bretmcgtinyurl.com/functions-lab | @bretmcg
● A user creates an account with email
account and password.
● A user signs in for the first time using
a federated identity provider.
● The developer creates an account
using the Firebase Admin SDK.
Authentication
Triggers
tinyurl.com/functions-lab | @bretmcgtinyurl.com/functions-lab | @bretmcg
● .onCreate
● .onDelete
Authentication
Triggers
tinyurl.com/functions-lab | @bretmcg
Let's get coding!
tinyurl.com/functions-lab | @bretmcg
Storage
Triggers
Trigger a function when data
changes in a Cloud Storage
bucket
hahaha
tinyurl.com/functions-lab | @bretmcgtinyurl.com/functions-lab | @bretmcg
● uploading, updating, or
deleting of files and folders
in Cloud Storage.
Storage
Triggers
tinyurl.com/functions-lab | @bretmcgtinyurl.com/functions-lab | @bretmcg
● .onChange
Storage
Triggers
tinyurl.com/functions-lab | @bretmcg
Machine learning as an API
Cloud
Vision API
Cloud
Translation API
Cloud
Natural Language
API
Use your own data to train models
Cloud
Speech API
Cloud Video
Intelligence
Cloud Machine
Learning Engine
TensorFlow
Two ways we can help you benefit from ML
tinyurl.com/functions-lab | @bretmcg
Vision API
Complex image detection
with a simple REST request
$ npm install @google-cloud/vision
Free tier!
Logo Detection
tinyurl.com/functions-lab | @bretmcg
Let's get coding!
tinyurl.com/functions-lab | @bretmcg
Database
Triggers
Trigger a function when data is
created, changed, or deleted
from the Database
lol
hahaha
lol
lol
tinyurl.com/functions-lab | @bretmcgtinyurl.com/functions-lab | @bretmcg
● All writes at the database path
● First write at the database path
● All writes after the first write at the
database path
● Delete at the database path
Database
Triggers
tinyurl.com/functions-lab | @bretmcgtinyurl.com/functions-lab | @bretmcg
● .onCreate
● .onWrite
● .onChange
● .onDelete
Database
Triggers
tinyurl.com/functions-lab | @bretmcg
Database Structure
messages: {
-K2ib4H77rj0LYewF7dP: {
name: "Jen",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg",
text: "lol that's awesome!"
},
-K2ib5JHRbbL0NrztUfO: {
imageUrl: "gs://friendlychat-833ed.appspot.com/GJHZV...D0A.JPG",
name: "Bret",
photoUrl: "https://lh3.googleusercontent.com/-xDoJpI/photo.jpg"
},
...
}
tinyurl.com/functions-lab | @bretmcg
Let's get coding!
tinyurl.com/functions-lab | @bretmcg
Thank you!
firebase.google.com/docs/functions
tinyurl.com/functions-videos
Bret McGowen
@bretmcg

More Related Content

Supercharge your app with Cloud Functions for Firebase