Cloud Functions for Firebase based timer - firebase

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.

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.

Can google firebase trigger receive google pub/sub message only once at the time

I currently working on a sync third party data project, that I decided to implement queue to ensure the message has received to the worker, but the worker need to do only one task at the time, which means the previous task should finished and acknowledged before executing the next one.
so the question is, how can I config the firebase pub/sub trigger to execute task one by one ?.
If I misunderstand on the concept of google pub/sub feel free to tell me :)
What you're trying to do isn't really compatible with the way pubsub functions work. There are other Google Cloud products that can help you limit the rate of invocation of some code, but it won't be implemented with a pubsub type function.
GCP offers a product called Cloud Tasks, which lets you set up a queue that can be configured with a rate limit. The queue can dispatch a task invocation to App Engine, or to an HTTP type Cloud Function (beta). So, if you've deployed an HTTP function, you could use its endpoint URL to configure a Cloud Tasks queue that invokes that function only once at a time.
You can control the number of instances of Cloud Functions with the MAX-INSTANCES flag of gcloud command line tool. See the documentation on Using max instances for full details.
The Firebase SDK for Cloud Functions doesn't wrap this option (yet). But you can deploy the function using the Firebase CLI, and then change the setting in the Cloud console, by following the steps shown in Clearing max instances limits.

Firebase - Cloud Functions : Always running functions

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).

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

Resources