Coding

Firebase Cloud Functions – Run Code on Demand

  • 00:00:02 welcome to this video great to have you
  • 00:00:04 on board in this video we'll dive into
  • 00:00:06 firebase cloud functions I'll explain
  • 00:00:09 what that actually is what what these
  • 00:00:11 are and of course we'll also see a
  • 00:00:13 practical example so let's dive right
  • 00:00:15 into it
  • 00:00:19 so what our cloud functions firebase
  • 00:00:22 cloud functions firebase cloud functions
  • 00:00:26 are all about server less event-driven
  • 00:00:28 code well that's a nice phrase but what
  • 00:00:31 does it actually mean it means that you
  • 00:00:34 can put some code in your firebase
  • 00:00:37 project firebase as you probably know is
  • 00:00:40 a provider as a service where you can
  • 00:00:42 host your own back-end or where you can
  • 00:00:44 build your own back-end without writing
  • 00:00:45 too much server-side code so cloud
  • 00:00:48 functions actually are a part of
  • 00:00:49 firebase where you can write your own
  • 00:00:52 server-side code and where you can
  • 00:00:54 specify when this code should get
  • 00:00:56 executed so let's have a closer look
  • 00:00:58 what does serve a less mean well
  • 00:01:00 firebase overall is all about being
  • 00:01:03 server less it means that you don't have
  • 00:01:05 to manage any servers of course
  • 00:01:07 there still are servers and wolff
  • 00:01:09 because it's code running somewhere in
  • 00:01:11 the cloud but you don't have to
  • 00:01:13 provision manage configure these servers
  • 00:01:16 instead you got all the environment
  • 00:01:20 working out the box and you don't have
  • 00:01:22 to worry about updating your software on
  • 00:01:25 your servers installing security patches
  • 00:01:28 or in general securing your servers you
  • 00:01:31 still will have to worry about writing a
  • 00:01:33 secure application and any code you
  • 00:01:36 write obviously should be secure but
  • 00:01:38 when it comes to setting up these
  • 00:01:40 servers and configuring the network that
  • 00:01:42 is all taken care of another advantage
  • 00:01:45 of this server less approach which just
  • 00:01:48 to make this really clear is not just a
  • 00:01:50 cloud function thing but the idea behind
  • 00:01:52 firebase in general so another advantage
  • 00:01:55 is the infinite scalability of course
  • 00:01:57 since you focus on your business logic
  • 00:02:00 and you don't have to manage any server
  • 00:02:02 resources you don't have to make sure
  • 00:02:05 that you provision enough servers for it
  • 00:02:08 as next like Friday sell your hosting on
  • 00:02:10 your website instead the server
  • 00:02:13 resources which are provisioned behind
  • 00:02:15 the scenes will automatically scale up
  • 00:02:17 and down and you only pay for what you
  • 00:02:20 need which of course is a great
  • 00:02:22 advantage and very cost efficient and
  • 00:02:24 finally with cloud functions now
  • 00:02:27 specifically we have clear separation of
  • 00:02:29 concerns we
  • 00:02:31 don't have to write all the logic we
  • 00:02:34 typically have to write on a server on
  • 00:02:36 our own the router the database access
  • 00:02:40 module all these things which do utility
  • 00:02:42 tasks for us we don't have to do that we
  • 00:02:45 focus on our business logic and we can't
  • 00:02:47 really separate the different parts of
  • 00:02:49 the business logic and build a micro
  • 00:02:52 service like back and where we really
  • 00:02:54 have individual parts doing their thing
  • 00:02:57 without having to clue them together now
  • 00:03:00 that's the server last part what does
  • 00:03:02 the event-driven thing mean here it
  • 00:03:04 means and that now is typical or Dennis
  • 00:03:06 a thing cloud functions really is based
  • 00:03:09 on that your code runs upon certain
  • 00:03:12 events so you decide when code should
  • 00:03:15 get executed now that of course has a
  • 00:03:17 couple of advantages
  • 00:03:18 we got predictable executions for
  • 00:03:20 example we decide when our code runs so
  • 00:03:24 we always know when it's going to run
  • 00:03:25 typical events would be like someone
  • 00:03:28 uploaded a file or maybe even someone
  • 00:03:31 sent the HTTP requests to a certain
  • 00:03:33 endpoint defined by you and we'll see
  • 00:03:36 all possible triggers in a second now
  • 00:03:40 another advantage of this event driven
  • 00:03:41 approach of courses we don't have any
  • 00:03:43 idle time we don't pay for our code to
  • 00:03:46 run and wait for incoming requests if
  • 00:03:49 there are non instead cloud functions
  • 00:03:52 only run when they need to and we only
  • 00:03:54 pay for when they run behind the scenes
  • 00:03:57 what happens is that firebase will
  • 00:03:59 provision the environment the cloud
  • 00:04:01 function requires on the Mond so that
  • 00:04:05 you don't have to pay for any resources
  • 00:04:07 you don't need and that is the third
  • 00:04:10 point here that the code environment
  • 00:04:12 only exists when needed and you don't
  • 00:04:14 have to manage that now the last thing
  • 00:04:17 of course to code well you only write
  • 00:04:20 what you need you focus on your business
  • 00:04:21 logic I touched on this when I talked
  • 00:04:24 about the separation of concerns so we
  • 00:04:26 focus on a business business logic we
  • 00:04:28 can use for party libraries we can
  • 00:04:30 absolutely do that you don't have to
  • 00:04:32 write everything on your own you can't
  • 00:04:33 build a normal JavaScript project
  • 00:04:35 because that's the third point for
  • 00:04:37 firebase cloud functions at least at the
  • 00:04:39 point of time recording this only note
  • 00:04:41 JSO server-side javascript is supported
  • 00:04:45 as a language but that shouldn't be a
  • 00:04:47 big issue because chances are if you're
  • 00:04:49 working with firebase you might be
  • 00:04:51 coming from a note Reyes background
  • 00:04:53 anyways or you already use JavaScript in
  • 00:04:55 a browser environment so then at least
  • 00:04:59 some of the things are already familiar
  • 00:05:01 so these are cloud functions now let's
  • 00:05:04 talk about these events that trigger
  • 00:05:07 your functions what are possible event
  • 00:05:10 sources well I'll get a couple of
  • 00:05:12 possible sources one is fire store which
  • 00:05:14 is firebase relatively new storage they
  • 00:05:18 always had this real time database and a
  • 00:05:22 couple of weeks ago or months I guess
  • 00:05:24 now they actually launched a new
  • 00:05:27 database you can use for essentially
  • 00:05:30 bigger amounts of data if your
  • 00:05:31 application scales up it offers
  • 00:05:33 advantages over the real time database
  • 00:05:35 now the core thing is fire store is a no
  • 00:05:39 sequel database and you can listen to
  • 00:05:41 you on create update delete or in
  • 00:05:44 general write events on write simply
  • 00:05:46 means either create update or delete so
  • 00:05:49 whenever new data is written for example
  • 00:05:51 your function can execute and you can do
  • 00:05:54 something typical example would be that
  • 00:05:56 you validate or transform incoming data
  • 00:05:58 let's say a user enters the username and
  • 00:06:02 in the cloud function you then want to
  • 00:06:04 check if it matches one of your
  • 00:06:06 blacklisted words and if that would be
  • 00:06:09 the case you could delete the entry
  • 00:06:11 after it had been added so you can add
  • 00:06:13 some extra logic that runs after that it
  • 00:06:16 has been written to the database for the
  • 00:06:17 first time it's the same for the real
  • 00:06:20 time database it's also supported you
  • 00:06:22 got the same hooks there and of course
  • 00:06:24 the same example so really the the same
  • 00:06:27 functionality for either database you
  • 00:06:30 chose now you're not limited to database
  • 00:06:34 triggers though as you know or as you
  • 00:06:36 might know firebase also offers an
  • 00:06:39 authentication module where you can let
  • 00:06:41 firebase manage the whole off flow and
  • 00:06:44 all the complexity that is involved with
  • 00:06:46 authenticating users so here for
  • 00:06:49 authentication we can also listen to you
  • 00:06:51 the creation or deletion of users and
  • 00:06:53 execute code when it happens an example
  • 00:06:56 here would be that a user signs up so on
  • 00:06:58 create
  • 00:06:59 is triggered and then we send a welcome
  • 00:07:01 mail now in the past without cloud
  • 00:07:03 functions that was pretty much
  • 00:07:05 impossible to achieve now you can simply
  • 00:07:08 listen to the user was created event and
  • 00:07:11 then run some code where you use a
  • 00:07:13 provider like let's say MailChimp or
  • 00:07:15 something like that that sends out the
  • 00:07:17 email we also have some google analytics
  • 00:07:20 related triggers now here I have to say
  • 00:07:23 I'm talking about Google Analytics for
  • 00:07:25 firebase which is an integrated product
  • 00:07:27 that builds up on Google Analytics and
  • 00:07:28 there you have so-called analytic events
  • 00:07:30 which you can define on your own which
  • 00:07:33 can in trigger in the browser so in the
  • 00:07:35 client-side code send to your server and
  • 00:07:38 store some information and now with
  • 00:07:40 cloud functions you can also listen to
  • 00:07:44 such an event or not just store it in
  • 00:07:46 the analytics database where it will end
  • 00:07:48 up anyways but also run some code for
  • 00:07:50 example write some extra information to
  • 00:07:53 your own database if you wanted that
  • 00:07:54 send the notification to yourself like
  • 00:07:57 hey and our user bought my product
  • 00:07:59 something like that we also get
  • 00:08:01 crashlytics an average service offered
  • 00:08:04 by firebase this is a service that is
  • 00:08:06 mostly relevant to mobile apps so
  • 00:08:10 Android iOS apps and you can then embed
  • 00:08:12 the firebase SDK and basically report
  • 00:08:16 issues or crashes through the internet
  • 00:08:18 to your back-end so that you have a live
  • 00:08:21 update of crashes and you don't have to
  • 00:08:23 wait for customers to write reviews
  • 00:08:25 there you also can listen to crashes or
  • 00:08:28 issues which were detected and then run
  • 00:08:30 some code here you could also write some
  • 00:08:32 extra data to the database or send a
  • 00:08:35 notification to yourself maybe saying
  • 00:08:37 yeah and now there has been another
  • 00:08:39 crash of this type I want to get an
  • 00:08:41 email something like that
  • 00:08:42 cloud storage is another very useful
  • 00:08:46 event source cloud storage AST firebase
  • 00:08:49 product where you can store files you
  • 00:08:52 could easily upload them to cloud
  • 00:08:53 storage through the firebase SDK but you
  • 00:08:56 can also upload them differently and
  • 00:08:57 actually that is one example I'll dive
  • 00:08:59 into in the next video I guess so here
  • 00:09:03 you can listen to on change events which
  • 00:09:05 occur whenever an object is added for
  • 00:09:08 example now whenever such an object is
  • 00:09:11 added this would run
  • 00:09:13 and then you could execute some code
  • 00:09:14 taking that object into account for
  • 00:09:17 example a typical example would be image
  • 00:09:19 optimization let's say a user uploads an
  • 00:09:22 image and you want you create three
  • 00:09:25 different versions in three different
  • 00:09:26 sizes now you can easily do that with
  • 00:09:29 the cloud storage trigger which
  • 00:09:31 basically tells you when a file is added
  • 00:09:33 then you check if that file is an image
  • 00:09:35 and if it is you simply resize it into
  • 00:09:38 the three formats you want to have pops
  • 00:09:41 up as another service offered by
  • 00:09:43 firebase it's basically a service which
  • 00:09:46 allows you to send a message to which
  • 00:09:50 you then can listen from from opera
  • 00:09:52 services in your firebase environment
  • 00:09:55 and whenever you publish such a message
  • 00:09:58 the unpublished trigger gets triggered
  • 00:10:00 and you can use that to to then do
  • 00:10:02 something additional maybe write some
  • 00:10:04 extra information to the database again
  • 00:10:06 now one super useful and versatile event
  • 00:10:10 source is HTTP HTTP obviously is no
  • 00:10:13 firebase service I'm talking about the
  • 00:10:15 normal well web here firebase cloud
  • 00:10:18 functions have one cool feature you can
  • 00:10:20 listen to an HTTP request that means you
  • 00:10:24 can essentially build your own endpoints
  • 00:10:27 API endpoints with firebase in the past
  • 00:10:31 before we had cloud functions this was
  • 00:10:34 not possible if you wanted to send rest
  • 00:10:36 so normal HTTP requests to firebase you
  • 00:10:40 had to send them to the endpoints made
  • 00:10:42 available by the real time database and
  • 00:10:44 that allowed you to do one thing post
  • 00:10:47 data to the firebase database or read it
  • 00:10:50 and so on but always you had to run code
  • 00:10:52 or essentially work on the database now
  • 00:10:55 you can create any API endpoint you want
  • 00:10:58 that runs any code you want and that is
  • 00:11:01 also an example I will show you so this
  • 00:11:04 is a cool feature that allows you to
  • 00:11:06 create API endpoints or restful
  • 00:11:09 endpoints without having to write all
  • 00:11:11 the rest like a router or anything like
  • 00:11:13 that so you can build a server less
  • 00:11:15 restful api these are the event sources
  • 00:11:19 that is what firebase is all about now
  • 00:11:22 in the next videos I want to have a look
  • 00:11:24 at using cloud storage events
  • 00:11:27 fire database events and HTTP events so
  • 00:11:30 let's have a look at all of that over
  • 00:11:32 the next videos