Check out a free preview of the full Firebase with React, v2 course:
The "Cloud Functions" Lesson is part of the full, Firebase with React, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Steve introduces what cloud functions are, and discusses use cases.

Get Unlimited Access Now

Transcript from the "Cloud Functions" Lesson

>> Steve Kinney: All right, we talked a little bit about the final thing which is cloud functions, which is a way to write, let's call it back end code. It's like serverless which is basically other people's servers for running code. Everything we have done has been in the client except maybe security rules.

[00:00:17] I guess security rules technically happened, but everything else has been in the client. One thing if you don't already have them installed is just there's a library virus functions which should have been in that MPM install and Firebase admin which is a way to do administrative things, right, without the security policies in place on your back end code.

[00:00:39] So, when we did the firebase and knit earlier, some of the groundwork was already laid for us. We have this Functions folder, which has its own package JSON. You can put anything from MPM in there that you need. If you need to do other stuff you can go ahead and do that, right?

[00:00:58] On the free plan, and this could change at any moment, but as of this writing on the free plan, you can't call 3rd party APIs, you can call any Google API which is pretty legit, like the facial recognition APIs. You can take a photo and do facial recognition, any other Google API can call but I think if you wanted to use [INAUDIBLE] to send an email I think you have to be on the pay as you go plan for that one, which is fine.

[00:01:25] I mean, if you had the plan and you're trying to do all that stuff, maybe it makes sense. The pay as you go plan is still pretty cheap as cost calculators is pretty cool. But you can install any MPM package you want in there, right? And so we're going to stick to mostly just vanilla JavaScript in this case.

[00:01:41] But any package you wanna use to do anything like image magic for resizing images, right, you could totally do that so that you can respond to it. It's called one of two things, that second one is many things. HTTPS requests, so you can effectively make an API. And then also you can listen for things that happen in firebase.

[00:02:01] Okay, I'm listening to this collection. Whenever a document gets added, I want to do this, or whenever a document gets changed, I want to do this other thing. So if you did need to store information in multiple places, you could say, okay, whenever the, this is a hypothetical, whenever the user changes their name, I wanna find all of the posts that have their name in it and change that too, right?

[00:02:26] Because it's interestingly in the way that we structure our database, which is, you could have a user profile and you could have that you ID in the post, but then every time you wanted to get the user information, you would have to go ahead and query for that document as well, which will cost you a query.

[00:02:47] Or when they write that post, cuz how often does a user change their username? All right, or even a photo and if it doesn't immediately, fine, right, you can batch it to once a week, right? You can include all the information in the post, that's not one request, right?

[00:03:01] And so you can then write the cloud functions, if they do change it, then I do wanna go back through and do it but that's way better than trying to do it on every single request. If it's a blogging platform, more people are probably reading that blog post than the user is changing their user name by orders of magnitude, right?

[00:03:22] So cloud functions let you to either a, handle a CCP request or b, they allow you to listen for changes to firestore, new files uploaded, a new user created the off system. Anything along those lines, you could hook into and have that trigger you doing stuff. We're gonna start with a built in example, just see this how this works.

[00:03:44] So you can see we've got this functions library here, and we also have this little example function. You can see this is functions. I'm gonna uncomment this one for a second. Exports.helloWorld is functions.https.onRequest. If you ever use Express or anything like that, this should look very familiar. So request object and response object, and this response is sending hello from Firebase.

[00:04:15] So we've uncommented that one.