[MUSIC PLAYING] TODD KERPELMAN: Hey, there, everybody, and welcome to a brand new Firebase Semi-Live Series Are you excited? I’m excited So I’m doing the series because in my previous Firebase conference video where I talked about how to turn on billing and still sleep at night, I ran you through, well, a series of things you can do to get more information about your projects overall billing usage and database usage and that sort of thing One of the things that I did was I turned on a billing PubSub that in turn kicked off a Cloud Function which went ahead and analyzed my overall billing usage and sent me generally sort of smarter alerts, in that case, through a Slack channel, if it turned out that my billing seem to be higher than expected, or just to send semi-regular updates I did all that very quickly and said, you know what? If you want to see the code, I’ll show you in a later video Some people did say, hey, I want to see that code a little better And so this is that video I’m going to basically show you how to set a billing PubSub and then use that to activate a Cloud Function, which then, in turn, can analyze your usage and hopefully send you some more intelligence alerts depending on what you are interested in what’s happening within your project A number of people also ask, you know, well, can you show me how to automatically disable my project if my billing usage gets too high? And while I am still not a fan of that idea for a number of reasons that I’ll get into when we get into that section, if I can figure it out, and hopefully we can because there is documentation, let’s try and get that working, too So maybe we’ll kind of, at the end of this, set up a little kill switch so that our project really kind of breaks if our billing usage gets too high Like I said, not a huge fan of that solution for a number of reasons, but there are cases where I could see that being useful So we’ll talk about that a little later As usual, I’ll start with my typical caveats, which is, like, we’re going to be doing a lot of stuff in TypeScript, and I am not a TypeScript and/or a JavaScript expert So if you see me doing dumb things or silly things and you’re like, huh Is he doing that because he works at Google or Firebase and knows something more than I do? The answer is no I’m kind of figuring this out as I go as well So if you notice that I’m doing something and could be doing it in a slightly smarter way, go ahead and ping me nicely in the comments and I’ll make those adjustments for future videos So with that, let’s get started All right So here we are inside my project here It’s called Semi-Live Money Spender Basically, what this is a series of Cloud Functions that go ahead and read in a bunch of documents and write a bunch of documents on a somewhat regular basis so that over the course of a day, I end up spending, like, $0.03 or $0.04 It’s just enough for me to have a somewhat accurate sense of my billing, and it makes these sample functions a little more useful I’m not going to show you how to set up this project itself I would say go ahead and use any project where you have billing setup Or if you don’t have any, go ahead and just turn on billing on your favorite project Ideally, if you have a project where you actually are spending money, that’s probably a little better because then you’ll get more interesting results But I am pretty sure most of this stuff could still work even if you are in a project that is getting charged zero dollars It would just be a whole lot less interesting So yeah, here you can see I’m just looking at my usage here Looks like I have just enough to get over the free tier at around 12:00 every day And I will probably get charged $0.04 or $0.05 at the end of the day So that’s nice So within my project, like I said, there’s really not much going on beyond the fact that I have a few functions in here And this is kind of hidden away because honestly, you don’t need to see this It’s kind of junky stuff That is functions that are being called via a cron job that are, like I said, reading in or writing a few thousand documents every time they’re being called I think the main point here is that I do have Cloud Functions setup and working inside my project as TypeScript I am using the TypeScript version of Cloud Functions, which, in general, we recommend because type safety always makes things easier to work with Hopefully, you have a project where you have this setup as well If not, I believe what you can do is sort of go into your Firebase project and type Firebase init functions And it will go ahead and ask you, basically sort of what you want to get set up Pick TypeScript hopefully as your language And I would say, yes, I want to use ESlint In my case, I’m going to Control-C out of it just in case it– I don’t think it’s going to override anything It’s usually pretty smart about that But just in case, I’m going to Control-C out because I already have everything set up appropriately Now, one thing I do want to note is we did recently switch from using TSlint to ESlint for our TypeScript functions And at least with the default configuration that I had set up, I ran into a few areas

where basically, I was getting some of these errors were reported to me within VS Code or by the lender Perhaps these are issues that have been smoothed out by the time you are using this Like I said, I think I started using functions just as they switched linters from TSlint to ESlint Or maybe these are things where you’re like, yeah Obviously, Todd Any TS developer would know this stuff But you know, like I said, kind of new at this But let me show you In case you run into errors like this where it’s like you’re sort of running into parsing errors, there’s a few things that I had to go in and fix The first thing is I had to go and add under my import resolver this section here, where we go ahead and add TS among all these others That fixed a few errors once I added that in And while this fixed a few errors, this did lead to a couple other strange errors where I’m getting, like, cannot read file, you know, blah, blah, blah. tsconfig.json Now, I was able to fix that by going into my ESlint RC file and adding this back in where I said, OK Well, let’s set our root directory to our directory name I think that basically, that sort of helped our linter find where these files actually existed That helped quite a bit, although I also– and I think you might notice this if you restart– I had to include eslintrc.js under my include list in my TSconfig.json file So those were three changes I had to make in order to get the new ESlinter working for my functions And like I said, I don’t know how much this falls into, like, obvious to anybody that does any amount of tinkering with type script, developers, or if this was sort of new stuff, or if you don’t even run into these errors But just so you know, these were a few things I had to do But where I’m at right now is I now have some Cloud Functions that I can go ahead and call And so– actually, you know what? This really probably isn’t necessary, but let’s do a little Firebase deploy only functions Just so we can kind of play this And we’ll call this Hello World function So I’ll wait a couple minutes And that’s funny Used to be, it would tell me where the ACP call was For instance, Hello World Looks like it’s no longer doing that That’s weird I wonder if there’s a reason for that or just a little blip But you know what? I’m pretty sure I can do that by going into my Functions Control Panel here And here we go Here’s the request that I’m going to make I’ll just copy and paste this into here And again, Hello from Firebase All right Yay, things are working So at this point, we’ve basically done nothing beyond sort of get our initial project set up and made one call to a Cloud Function This should be a good starting point for all of us All right, so let’s go ahead and actually start by activating our billing PubSub Now, PubSubs, and I explained this a little, probably a little more eloquently in the other video, but a PubSub is basically a sort of somewhat generic message passing service It allows you to pass messages, generally defined as JSON objects, through various Pub/Sub channels And any service that is authorized to listen to that channel can go ahead and sort of receive those JSON messages as they’re being sent The nice thing about PubSub is it’s really kind of good at handling bursty bits of traffic If all of a sudden, you have a bunch of messages being sent at once, it can sort of handle those and keep them in a little queue as yoou process them And it’s sort of, generally speaking, a nice, kind of platform-independent way of sending messages from one service to another At least that is my overly simplified explanation I’m sure the Google Cloud folks have a better explanation But one of the things that you can do with a PubSub is set up a billing PubSub where our billing service will actually go ahead and send one of these billing notifications to a PubSub channel that we can name So let’s go ahead and do that So yay! Getting into some real code here So first thing I’m going to do is let’s go under usage and billing And actually, can I go ahead and, billing account And under Budgets and Alerts– this is probably the easiest way to get to it– I’m going to click View Budgets And this will take me into the Google Cloud platform billing channel And as I mentioned before and as you probably know, every Firebase project is also a Google Cloud project underneath the hood And so basically, I can go ahead and address some of the more advanced features of my Firebase projects right here in Firebase So here is my money spender Actually, it’s this one here This one labeled Semi-Live Money Spender I’m going to go ahead and click on this

Here, you’ll notice I have a few email budget alerts These are the things that essentially I set up through a little dialog when I first turned on billing for the project Maybe you remember doing that Maybe you don’t But the thing that we’re interested in is this– connect a Pub/Sub topic to this budget So I’m going to click this And I’m going to select a Pub/Sub topic And in this case, I can just– I’m going to go ahead and take one I think before– oh It says I need the PubSub topics Create permission, that’s a little weird You would think I would have that since I am the only owner of this project I wonder why that is Fascinating Huh Well, let’s see Let’s make sure I’m in the right project here I’m in Semi-Live Money Spender No, sorry Wrong project OK Well, that was interesting Apparently, even though I am in the Firebase project– and you know what? Let’s give this a better name I hate the fact that all my project names are just named Firebase project We’ll call this Semi-Live Money Spender The fact was I actually had to actually switch, not to this one, which was sort of my old money spender project, but this one Semi-Live Money Spender I had to actually switch to that before I could create a topic For some reason, I just had no projects created Unusual, but there you go So I’m going to say Create a Topic And it used to be– I think the last time I did this, I called it Billing PubSub And I realized that name is kind of redundant Like, you’re already creating a PubSub topic, so calling it Billing PubSub is sort of unnecessary So I’m just going to call it Billing And we are not going to use a customer managed encryption key I don’t even really fully understand how that works And since I tend to not mess with encryption that I don’t understand, I’m just going to let Google manage the encryption for me So I’m going to hit Create Topic And there you go Make sure you hit Save And now, within my Semi-Live Money Spender, I’m basically now getting periodic notifications through my project Semi-Live Money Spender topics Billing PubSub All right So now, I need to write our Cloud Function that’s going to go ahead and listen to this topic And if you were to look at our documentation– let’s see Cloud Function PubSub trigger Actually, here Cloud Function Billing PubSub I think that’ll get us to the page Yes– Examples of Automated Cost Control Responses And by the way, this is the documentation I’m essentially using as sort of the framework for most of what I’m doing It’s not like I know the stuff off the top my head I’m just mostly reading the docs You might notice, by the way, that the functions here are a little different than what we’re going to do in Cloud Firestore Or sorry, in Cloud Functions Firebase Cloud Functions simplifies a little bit of the process in terms of sort of deploying functions and also sort of maybe more simply listening to various PubSub topics So this actually looks a little different than the code we’re going to do, but the basic principle is the same So here, let’s just go ahead and write the code You can see what we’re doing here So exports Let’s call our function receiveBillingNotice And I’ve already gone ahead and imported functions from Firebase functions We’ll call it functions.pubsub And then we’re going to pick our topic And in our case, I believe we called it billing And then onPublish And this is basically going to be a function that takes in a message and does something with it So let’s see What can we do here? I guess let’s go ahead and print it out So here, data equals message.json And this will be essentially the payload of the PubSub notification we’re receiving Let’s console.log Received pubsub notification And we’ll console.log the data So you know what? Let’s deploy this thing and test it

Yeah, let’s do that. / All right So we’ll do a firebase deploy only functions I will wait a little bit for this to deploy And there we go All right That took a while So we have our function deployed How do we test it? So I suppose one answer is just to kind of wait a while I find that these billing PubSub notifications tend to arrive, like, every 20 minutes or so, although I think the first time you turn on a billing PubSub for your project, it takes a couple hours I think for the PubSub to sort of start up But then after that, you get it, like, every 20 minutes or so However, I don’t want to wait that long That would make for a pretty boring video Luckily, the documentation does tell us how we can go ahead and test it So testing your Cloud Function This basically is kind of the payload or what a typical billing notification payload might look like And to do that, we can publish a message in PubSub using this object as the message body So I guess let’s figure out how to do that So let’s see here So it looks like there’s a couple of ways of publishing messages Well, lots of ways The way I’ve been doing it has been to use this GCloud Command Line tool to call GCloud PubSub topics published But this is funny I didn’t notice this before Maybe this is new or maybe I just never noticed it, but it looks like you can also do it directly from the Google Cloud Console, which might be a little easier Let me show you both ways So I believe I already have the GCloud Command Line Tool installed GCloud version Yes, I do If you don’t have this installed, probably the easiest way is do a search for, like, GCloud CLI Install And you should get various options for whatever platform you’re on And this will go ahead and install everything for you, hopefully And then I think just to make sure that I am properly signed in to this account, I’m going to call gcloud auth log in And that will go ahead And yes, I want to use this account And yes, I’m going to go ahead and authorize the command line tool And hooray, that’s fantastic And actually, I guess the next thing I want to do is go ahead and set the right project by calling gcloud config set project So let’s do that And let me just make sure I get my project ID name properly here, Let’s go to Cloud Sorry Hold on Let’s go to Cloud Console And here we go There’s my project ID Semi-Live Money Spender All right So I now have my project properly set up And now, I can call gcloud pubsub topics publish And here, I’m going to type in the name of my PubSub topic, which I believe is billing And then message And then here, in quotes, I’m going to actually put– and let’s see if I can find this– the sample message So here again is the sample payload I’m just going to copy and paste this into my command line End it with my single quote And I think that should do it How do you know of it worked? Well, let’s go and check out our Cloud Function log So over here in functions I’m already here You know what? I can probably even– let’s just look at logs just for my received billing notice function And here we go So here’s the function I just sent And you can see basically, all it’s really doing is spitting out the key value pairs that I am getting within this notification So that’s nice It’s also telling me apparently function returned undefined Expected promise or value I think you can just probably fix that by adding in return here, and I won’t bother you with deploying this But this seemed to work Well, let me– let’s look at the other way of doing it as well, which is let’s go ahead and use the console So let’s see Let’s go to the PubSub Topics page on our platform Let’s make sure we have our project Semi-Live Money Spender And I’ll click on Topics And there is my topic ID Fantastic And I’m going to go ahead and click Publish Message, which will publish a single message to this topic And let me go ahead, and– I think I already have it copied in my clipboard But let me copy this again And I’ll paste this And just to make sure, you know, I’m not looking at the logs from my CLI tool, let’s call our budget Budget From Console

And we’ll hit publish And once again, hopefully, if I go back to my logs, wait a moment or two, and there we go This is the new PubSub topic that I received with my Cloud Function You can see because the budget display name is now Budget From Console So look at that We have basically successfully created a PubSub topic that is linked to our billing account, and we have created a Cloud Function that is set up to receive those topics Again, you’ll notice that I haven’t actually shown the full, complete cycle where we’re getting real topics from my billing channel because I believe I would probably have to wait a couple hours for that to get going, and that would make for a really boring video But I think for the purposes of development, we’re OK and can probably continue to move on So I realized the first semi-live video that I do is never very interesting It’s always kind of like, hey, let’s do a lot of setup work But we’ve accomplished a fair amount I think already in these 20 minutes or so And now, we’re set up so we can actually start doing something a little more interesting with these billing notifications as we receive them through our PubSub channel And we’ll talk about doing a little more of that in the next video So I’ll see you soon Thanks, everybody [MUSIC PLAYING]

You Want To Have Your Favorite Car?

We have a big list of modern & classic cars in both used and new categories.