[MUSIC PLAYING]
DOUG STEVENSON: Hey, how's it going, Firebase developers?
My name is Doug Stevenson, CodingDoug on Twitter,
and I'm here to get you started using
Cloud Functions for Firebase.
I'll assume that you already know about using Firebase
on Android, iOS, or the web.
Any mobile platform is OK because we're
going to start writing back-end code for your Firebase app,
so it doesn't matter which platform your app is on.
Cloud Functions is part of the Google Cloud Platform,
and it lets you write and deploy code
that responds to events from Google Cloud Services,
including Firebase products that you might be using.
With Cloud Functions, you can sort
of glue together parts of your app
that are built with Firebase.
And all your code runs on Google servers,
not inside your mobile app.
So this is a great way to keep your application
logic centralized and safe, all without having
to manage your own servers.
That's right.
You don't have to set up any servers,
scale them up or down to meet user demand,
or do any maintenance on them.
You just write and deploy code that responds to events.
Some people call that serverless architecture because you never
have to worry about or even know about the servers that
are running your code.
The way Cloud Functions works is like this.
Your code is triggered in response
to events coming from Firebase products.
For example, writes to Realtime Database or Firestore
can trigger a function, as well as file uploads
to your Cloud Storage bucket.
You can respond to new user accounts created
by Firebase Authentication and conversion events
in Google Analytics for Firebase.
Also, you can respond to incoming web requests
over secure HTTP.
Now, there is a lot to unpack here.
And it's more than I can cover in a single video.
But I'll get you started deploying your first Cloud
Function so you can experiment with it.
And we'll do it in JavaScript, running
in the Node.js environment.
Actually, we'll do it in TypeScript,
which is a lot like JavaScript, but much better.
If you don't know TypeScript or JavaScript, that's OK.
Maybe now is a good time to start learning.
And we do plan on supporting more languages in the future.
But before writing any code, let's
go over the things you'll need to do first.
OK.
Create a Firebase project if you haven't done so already.
You can create one at the Firebase Console with the link
in the description below.
Next, you'll need to install Node on your computer.
There are instructions for that if you go to Nodejs.org.
The link is also in the description.
There are instructions for Windows, Mac OS, and Linux.
After you install Node, the instructions in this video
are mostly the same for each platform.
You could also use something called Node Version
Manager, or NVM, to download and manage
Node on the command line.
Personally, I'm using Mac OS, and I also
happen to be using a package manager called
Homebrew to make Node installation and upgrades easy.
Whatever you choose, you should get a recent version of Node
and make sure it gets installed in the path of your command
shell.
Then, you can check your version of Node
on any system like this.
You should have at least version 6.
Also, your node installation should
include NPM, which stands for Node Package Manager.
You can check the version of it as well.
You should have version 5 or later.
Once you have Node installed, it's
time to get the Firebase Command-Line Interface.
You may already have this if you've
worked with other Firebase products on the command line.
But you should get the latest version
to work with Cloud Functions and keep it up to date.
So let's do that.
You can install the Firebase CLI with NPM like this.
Be sure to use dash g to install this module globally.
On Linux, depending on how you have Node installed,
you may have to run it with Sudo to make
sure the installation completes successfully.
When that's done, you can also check its version.
OK, now, you have all the necessary software
installed to do development with Cloud Functions.
It helps to keep these tools up to date
so you can stay on top of future changes.
The last tool you might want to think about installing
is a code editor for TypeScript.
You can use any editor you want, but I
use VS Code, which is popular with the Firebase team,
because it offers helpful code assistance.
After you've got your development tools set up,
you'll need to initialize a workspace folder for your Cloud
Functions code.
I'll use a new folder within my home directory.
At the command line, I'm going to create and change
to the directory I want to use.
In order to work with my Firebase project,
I need to log in with an account that has access to it.
The command Firebase Login will kick that off and open up
a tab in Chrome.
It's asking me for some permissions, and I'll allow it.
Back at the command line, the next thing I need to do
is run Firebase Init to create some project scaffolding.
When I do that, it asks me what I want to set up.
Right now, I only want to set up functions,
so I'm going to use the arrow keys and space
bar to select functions and then press Enter to proceed.
Now, it's asking me which of my Firebase projects
I want to deploy to by default. I'm again
going to use the arrow keys to choose the project
I want, then, press Enter.
Now, it's asking me which language
I'd like to use to write my functions.
As I said before, I'm going to use TypeScript, since it's
the preferred language.
And now, it's asking me if I want
to use TSLint to catch bugs.
I definitely want this because the advice of this Lint tool
is very helpful.
I've written a blog post about this, which I strongly
recommend that you read later.
The link to that is in the description below.
Now, it's creating some files and folders here.
And it's asking me to install some dependencies with NPM.
I do want them because I'm going to use those modules to write
my functions.
So I'll hit Enter again to make that happen.
And now, it's installing those modules with NPM.
And the last thing it did was write
a couple more files to finish configuring my project.
OK.
Firebase Init did a lot of stuff here.
So now, we'll take a quick tour of the files
it created so you can understand how your Cloud Function
project works.
Open up VS Code at the root of this project.
First, I'll look inside the Functions folder.
There's three important things in here.
Package.json is a file used by Node to describe my project.
You can see here that it has dependencies for Firebase
Admin and Firebase Functions.
These were the modules that NPM installed for me
earlier, along with all of their dependencies.
If I choose to install more modules to use in my functions,
they'll show up here.
Back at the command prompt, I'll do one more thing just
to make sure I have all the latest code.
I'll run this command to update those modules to the latest
versions, so I have all the most recent code.
Next, there's a folder called Node_modules.
This is where NPM installed those modules earlier.
You can see there's a big chunk of stuff here.
And there's one thing to bear in mind about this folder--
you probably don't want to check in any of this
into your project source control.
NPM is supposed to manage this folder for you.
Since I don't want anything from this folder in my source
control, I'm going to add it to a gitignore file
so Git won't bother with it when I commit my code.
Now, the last file is an Index.ts in the source folder.
This is the entry point for defining all your Cloud
Functions.
You will most definitely start writing code here.
To make this tutorial easy, I'm just
going to uncomment the sample function here
that it created for me.
Of course, it's a Hello, World sample.
Let's walk through what it does.
First, it imports the Firebase Functions module
and makes it available via this functions identifier.
You'll use this to build the functions you want to deploy.
Then, it exports a function called helloWorld.
It's defined using the functions identifier from above,
and it's an HTTP-type function.
HTTP functions let you respond to incoming web requests.
When a web request occurs, the function
triggers with a request and a response parameter.
These actually come from a Node module called Express.
So if you already know Express, you'll be right at home here.
Then, in the body of this function,
I'm going to send the string back to the client.
That response ends the function.
And that's all there is to it.
It's important to know that the Cloud Functions
runtime actually requires JavaScript code to run.
But since I'm using TypeScript, it
has to be compiled into JavaScript
first before deployment.
The Firebase CLI will do that automatically
when you run the deploy command.
The default configuration takes all the TypeScript
from the source folder and compiles it to JavaScript
in the lib folder next to it.
You don't need anything from the lib folder checked
into your source control, so you should add it to your gitignore
as well.
OK.
Let's deploy this code.
Back at the command line, I can use the Firebase CLI
to deploy like this.
You can see that the TypeScript is compiled into JavaScript,
and that's being sent to Cloud Functions.
When the command finishes, it gives me
a link to the console for my project
and also the URL for the HTTP function that I just deployed.
If I copy that URL, paste it into my browser,
it'll invoke the function.
And I see the response that it sent.
Now, I'll go back to the code, change the welcome message,
and also add a console log, which I'll find later
in the Firebase Console.
Then, I'll deploy the function again, just like last time,
and reload the browser.
And now, I see the new message.
But what about that console log?
To see where that went, I can go to the Firebase
Console for my project, select Functions on the left,
and choose to View Logs for my Hello, World function.
And here, I can see the log message.
You should use console logs like this to help test
and debug your functions.
And that's your first Cloud Function.
But there's so much more you can do with this,
as I mentioned before.
So to learn more about Cloud Functions for Firebase,
be sure to read the documentation,
follow our Codelab, and check out our sample code in GitHub.
The links are all in the description below.
And be sure to subscribe right here on the Firebase channel
on YouTube to get notified of new Firecasts
about Cloud Functions and other Firebase products.
And I'll see you here next time.
[MUSIC PLAYING]