How To Write Cron Jobs In Firebase For Your Flutter App - Scheduling Time Specific Cloud Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you [Music] what's up everybody welcome back to another episode of trade codes as always my name is Trey and today I'm here to give you more content related to flutter and Firebase okay so specifically what I want to talk about today is a way that you can automate the process of different functions within your app and how do you automate that well of course you can figure out ways to do it within the app itself but you can do it in a cloud which makes it even more easy and it works seamlessly all the time the way you would do that is by creating a cloud scheduler function but let me first preface this with what is a cloud function so a cloud function is essentially a function that works in the cloud whenever you need it to execute for whatever reason you might have a cloud function that sends push notifications or sends users to a payment page using the stripe API or it may be some type of cloud function that does an interaction with a database like mongodb so Cloud functions are a way that you can pretty much add some functionality in the back end for whatever app you're working on and it makes it very easy for your ad a cloud schedule function is those same Cloud functions but now they work at a specified time right and so it's essentially a Cron job what that means is you can automate the process by not having to actually call the cloud function but instead the cloud function calls itself whenever that time is specified and you can modify these increments of time to whatever you need them to be you can run these Cloud functions every day every week uh every hour every Friday at 5 PM or you can even run them every minute and that's exactly what we're going to do today I'm going to create a cloud scheduler function that runs every minute and then we're going to create a flutter app that is going to be interacting with the data that's been updated from that cloud scheduler function all right so let's go ahead and jump into it alright so first thing that we need to do is we need to make sure that we have a Firebase project already created and I have one right here for my demos project now keep in mind that you don't need to create a Firebase project specifically you really just need to create a project for to use the Google Cloud functions console so you can actually use the console separately from this but the cool thing with Firebase projects is when you create a Firebase project it automatically syncs with those Cloud functions in the console so you could either use the native Google Cloud functions console or you can use Firebase since I work with Firebase all the time I figured it'd be good just to use this for the example so just keep in mind that you can use either or but for this example I'll be using Firebase then if we go over to functions we can see that we have just a couple functions from some other projects that I was working on but this is essentially where we will be putting that cloud function that cloud scheduler function this is where we will be putting it at all right so now what we need to do is we need to go ahead and get node installed as well as the Firebase CLI which will allow us to deploy our Cloud function to the cloud all right like I said we need to go ahead and install node.js this is a JavaScript run a JavaScript runtime environment and this is where we will be creating our Cloud functions so our Cloud functions essentially need this to to work in the server so you go over to nodejs.org download the latest version pretty simple to set up so I already have it set up so I'm gonna skip that step but once you have that you'll want to come over to your actual uh project your Firebase project and I'm in the root directory Here and Now what we want to do is we want to install the Firebase CLI this is going to give us the tools that we need to make this function come to life so I'm going to run sudo npm install Dash G Firebase tools alright and I was going to fetch all the packages no modules all that good stuff for us to use for this project make sure you throw suit on there because it's most likely going to ask for a permission to do this installation all right great we have everything we need now what we need to do is create a web directory where we will put our Firebase functions in and we'll go ahead and do that right now all right now we need to go ahead and initialize Firebase functions and the way that we do that is first I like to create a web directory the reason I do that is because there's already an Android and iOS directory with a flutter project so I just create the web directory to Encompass anything that we need on the website including JavaScript functions for our Cloud so with that director we created we're going to CV into that and run the command Firebase init functions all right so first is going to prompt us do you want to use an existing project create a new one at Firebase when existing Google Cloud platform project don't set up a default project we're going to select you an existing project and we're going to use that demos project that I specified earlier what language would you like to use to write Cloud functions I'm going to select JavaScript you can use typescript if you want do you want to use eslint to catch probable bugs and enforce style I'ma put no because that linter is extremely strict you almost have to have Flawless code in order to push up a cloud function and I don't want to deal with that today so I'm going to put no for that do you want to install dependencies with npm now wait yes all right and now I was going to go ahead and fetch all the modules and packages and everything that we need to use Firebase functions all right so with everything installed Firebase initialization complete we can go ahead and look in this web directory and we have the functions directory and we have our index.js file this is where we will be adding our Google Cloud function to be pushed up to the server okay all right right so in order to save us a little bit of time I went ahead and wrote out the cloud function already but I'll just go ahead and explain what everything is in here so within that index.js file we have the functions import and the admin import first thing we need to do is initialize our app this allows us to actually make interactions with our Firebase project then we create our Cloud function by typing in exports and then we create the name of our function so this essentially says that my scheduled Cloud function will be one of the functions that is going to be in our Google Cloud function lineup essentially so then we call functions.pub sub dot schedule this is where we actually specify the pattern of how often This Cloud function is going to operate you can come over to a website called trontab.guru and it lets you know what pattern you would need in order to create the time increment that you would want or how often you would want your function to run so if I select random right here you'll see that 50 star 8 star is at 1205 in August so this will only run once a year right so um just keep in mind that that if you can come over here if you want to actually modify patterns and see what what it would look like in order to specify it in your app so since we're running a cloud function every minute we're going to add all five stars that means it's going to run every minute then we specify the time zone America New York and then add the on run property right here and so now everything in here is going to be executed every minute all right so just keep in mind that this is essentially the cloud function that we're working with this right here is the scheduler function that's going to execute that cloud function every minute so what we're doing is I have a document in Firebase a collection called CF data in the random document and then a property called coin so what this Cloud function is going to do is it's going to update this coins property by five five coins every minute all right in order to do that we get a reference to that document by calling a weight admin.firestore.collection specifying the collection and in the document which is the idea of the document and then calling get so this is the document right here then a little title here then we need to convert that dot to Json so that we can work with it so we will call cfdata dot dot data and then we need to update the amount of coins by five on that document so we'll call cfdata dot dot dot rep dot update pass in the key that we're going to be updating which is coins and then we need to call admin.firestore.fieldvalue.increment5 the reason you use this field value to increment is because it will automatically add whatever value you specify to the already pre-existing value for that coins property if we just passed in five right here like if we just did this then it would update the coins property into five every minute and we don't want that we want it to increase we wanted we want it to increase by five every minute so we'll call admin we'll call it the field value probably right here and then just return no all right so that is our Cloud function once we're done with that we need to deploy it to the cloud so we'll run uh we need to go into the functions directory and run fireplace Firebase deploy dash dash only functions uh big typo hold on all right so now what's the point our function to the cloud for us would you like to proceed with deletion no this is just saying that I have a function that is not currently in there or some extra stuff it was kind of my project but it's not in my local source so I don't want to delete it so I'll just hit no and continue with the deployment as you can see it says creating node.js function my schedule Cloud function something to keep in mind is I just noticed we didn't use this data object because we don't need anything else on it but ideally it's let's say you wanted to obtain that information to say I don't know get the property of someone's name and then modify it a little bit and then update that so you can use that method to update their name I had this in there just because I like to have access to the document already but you don't need right there all right it says project console deploy complete if we come over to Firebase and check our functions we should see my schedule Cloud function all right so now that the cloud function is done let's hop over to the flutter app and actually demonstrate what we need to do on the flutter side to get this app running cool now we're in the flutter app the demo page more specifically this is where we'll be actually coding the app so keep in mind that in Firebase I created that document right here on the CF data collection and we're updating that coins property by five coins every minute as you can see the coins amount is at 95 so that means ever since that cloud function was pushed up it's been executing every minute incrementing by five so if I do the math right that means it's been about 19 minutes since it's been pushed up or 20 minutes to be more accurate all right so we come back over to the flutter app we need to create a reference to that document simply by call calling Firebase firestore.instance collection specify the collection which is CF data and then the dot which is the document ID and then the only thing that we're doing here is since it is being updated every minute we can create a stream Builder and just listen for changes to that coins property which we're doing right here so we create a stream builder of type document snapshot the stream is going to be that documents references snapshots then as always in the Builder we want to make sure that while that snapshot is waiting then we'll just have a circular progress indicator up outside of that we want to extract the data from that snapshot which is that document itself converted to a map of string and a map of type string and data so we have that right here in this data object and then in the center of the page we're just displaying text that says coins and then the amount of coins that they have right and we do that with this right here okay so that's all you need to do on the flutter side I'm gonna go ahead and demonstrate what that looks like in the flutter app and you can watch the coins being updated every minute cool so this is the flutter app as you can see right now it's 8 10 p.m the amount of coins we have is 110 so once it strikes 811 the coin should say 115. so I'm not going to have you sit and wait in here I would love to insert the little SpongeBob five hours later man but just know that it's going to update in the next minute one eternity later all right so it's 8 11 now and the amount of coins we have is 115. so the app and the cloud scheduler function is working exactly how it needs to for this demonstration so with that said that is how you can create Cloud schedule functions for your app all right I do this in a few areas and a few of my apps one of my apps critic I used it whenever no not critic I'm sorry there was an app called score Square the one that I worked on the sports betting had essentially what I did there areas I wanted to reward users of the app with 50 free coins every Friday at 5 PM so I used a cloud scheduler function in order to execute that and handle all the logic on the back end so as you can imagine automating your process is super simple super easy and saves you a bunch of time so if this video was helpful as always please like comment subscribe let me know in the comments what you think about Cloud scheduling functions and let me know any examples of where you may have used this in your app alright I hope this video was helpful and I hope you enjoy the rest of your day until next time I'll talk to you later peace [Music]
Info
Channel: Trey Codes
Views: 1,699
Rating: undefined out of 5
Keywords: firebase, webdev, app development, typescript, javascript, lesson, tutorial, cloud functions, firebase cloud functions, gcp, cloud scheduler, pubsub, pubsub function, cron, cron jobs, crontab, time trigger firebase cloud functions, twilio, google cloud, cloud computing, cloud technologies, gcp exam, gcp associate exam, google associate exam, cloud engineer, google cloud scheduler, cloud scheduler cron, gcp cloud scheduler, gcp scheduler, cloud advocate, google cloud function
Id: Br0FuEuYqF4
Channel Id: undefined
Length: 13min 50sec (830 seconds)
Published: Fri Oct 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.