Firebase - Cloud Functions : Always running functions - firebase

I am new on firebase cloud functions. I would like to ask a question about always running or self-triggering functions. How can we handle these things? How can we implement always running functions or self-triggering?

Google Cloud Functions are snippets of code that run in response to events that happen somewhere else. Some example events:
a HTTPS URL is accessed, either from application code, or in some other way
a User account is created on Firebase Authentication
a node is written in the Firebase Realtime Database
a message is sent to a Cloud PubSub topic
There is no concept on a forever-running function on Cloud Functions (nor on other, similar Functions-as-a-Service offerings), although it's definitely possible to create a function that gets triggered every minute or so (like a cron job).

Related

Firebase PubSub Trigger with Message Ordering

does anyone know how to enable the Message Ordering property on a subscription when working with Firebase and NodeJs?
If I navigate to the GCP console and open the queue's subscription detail, I can change the parameter manually but I'd rather have it done when my functions deploy.
This link (https://cloud.google.com/pubsub/docs/ordering#enabling_message_ordering) also talks about a parameter that can be provided when creating a subscription but that doesn't seem to be available the way I usually create/deploy my functions.
GCP Documentation
Own Code
It sounds like you're asking if pubsub functions deployed with the Firebase CLI (and not gcloud) can request ordering. Currently, this is not possible. When you deploy a pubsub function with the Firebase CLI, it will automatically create the pubsub topic for you. However, there is no option to set the ordering setting.
If you would like see this implemented, you should file a feature request on the Firebase CLI GitHub and/or contact Firebase support directly.
Alternatively, you can switch to use gcloud to establish the trigger and create its pubsub topic with your customizations.
You should note that enabling ordering does not necessarily ensure that function invocations will be perfectly serialized with respect to incoming messages. Cloud Functions is designed for incoming events to be handled in parallel as fast as possible by using multiple server instances as needed. There is no guarantee that pubsub messages will be processed fully in order. The ordering only applies to pubsub topic consumer code that runs in a single process. You could try to set the max instances of a function to 1, but that is not a hard guarantee.
If you need fully ordered serialized execution of some code in response to pubsub events, maybe Cloud Functions isn't the product you want to use. Maybe a single App Engine or Compute Engine instance would work better.

Firebase and GCP Cloud Tasks for scheduled posting

I am looking to integrate an automated posting service in my firebase application. The users will create a post with a desired posting time from the client application which will be added to my Firestore database.
I would like to be able to create a Cloud Task to actually add the post to the client dashboard at the desired time, which could be weeks/months in the future.
Is a cloud function Firestore trigger that creates a cloud task the best implementation?
I know that Cloud Scheduler/ Pub/Sub /App Engine is normally the flow recommended for functions run on a normal schedule, i.e once daily/weekly. But I am looking to allow my users to specify the exact time they want their post to be sent.
Is my thinking to use Cloud Tasks correct?
Any insight would be appreciated!
I think that best approach is to use the Cloud Functions for Firebase client SDKs that will let you call functions directly from a Firebase app. To call a function from your app in this way, write and deploy an HTTPS Callable function in Cloud Functions, and then add client logic to call the function from your app. Then, if you want to schedule functions to run at specified times, use functions.pubsub.schedule().onRun() This method creates a Pub/Sub topic and uses Cloud Scheduler to trigger events on that topic, ensuring that your function runs on the desired schedule.

Mix Cloud Functions w/ Firebase

I have some pub / sub setup on Google Cloud and I have some Cloud Functions running on Firebase.
I'd like to set a trigger on Firebase, so when a user account is created, I publish a message to a topic on Google Cloud.
Is this possible? Am I missing something obvious?
I can trigger a cloud function on account creation and I'm happy at this point showing a console log.
I was considering in my Firebase trigger add something like #google-cloud/pubsub so I can literally setup the message in the body of my firebase trigger, but that feels a little wrong.
Quite clunky and not the way it should be done?
In short, I guess what I'm trying to do is firebase publish to topic on trigger?
I don't think there's anything wrong with what you're suggesting as a solution. You can use an authentication trigger to find out when a new user account is created, then use the node Cloud Pubsub SDK to turn around and publish a message to your topic. There's really no more straightforward way to accomplish this that I can think of.
It's pretty common to mix Cloud APIs into your Cloud Functions, regardless if they're based on Cloud or Firebase events.

Does Firebase has direct cloud function trigger?

In times when Parse.com was on they had a function that called a cloud function directly and returned whatever I wanted. So I could have all the server logic on the server, not in client code. Doe's Firebase has it as well? I can't find it - all I found are HTTP triggers, but it implies that it's not available through Javascript SDK. Am I missing something or do I have to use REST interface for that?
To run server-side code that is triggered by events in Firebase, you'd use Cloud Functions for Firebase. Currently these can trigger code through the Firebase Database updates, Authentication, Analytics, Cloud Storage and HTTP. The documentation I linked has all the details.
As Frank van Puffelen has explained you could use Functions, I'll like to add a couple of things.
You could also use the Firebase Database Admin SDK for the Database, this requires you have a server
Firebase Functions is a sort of big brother, constantly listening for whatever you want. Currently starting and deploying functions is fairly easy and fast, I love this how to video. Basically, you have to install the CLI and then using commands, create a project, write your js for Functions and for deploying those changes to Firebase Functions use the CLI again
Functions can listen more than what the Admin SDK can, the Admin SDK is for the Database while Functions is for, authentification, database, and cloud messaging. This means any user registration or deletion or any change in a node, can trigger further logic. This logic could include sending push notifications. There is a github repo where you can see a lot of examples, I made myself a small repo for the same purpose

Cloud Functions for Firebase based timer

So I know that Cloud Functions for Firebase don't natively support cron jobs, and one have to depend on an external source to trigger an HTTP cloud function. But I don't think this will work in my case.
I have an app in which users can create competitions that end in 24 hours, thus each time a competition is created, I need to set up a timer that triggers one time after 24 hours.
Do you guys have any idea on how this problem can be approached?
At this moment the solution that the Firebase guys are providing for solving the cron issue is to use Google App Engine to deploy a server and trigger the functions via Pub/Sub hooks. Here you can find the tutorial.
Basically you have to:
Create a Cloud Function that responds to a PubSub hook.
Deploy an App Engine that will publish an event depending on the cron setup you set.
If you don't want to setup an App Engine you can use an external resource like cron-job.org that can call your HTTP Cloud Function.

Resources