Firebase and GCP Cloud Tasks for scheduled posting - firebase

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.

Related

How can I schedule firebase messaging to send a message at a specific time everyday?

I would like to use firebase messaging to schedule a message everyday, at a specific time(say 9PM). How can I achieve this? Also, is it possible to achieve this without using firebase cloud functions?
Thanks in advance.
Is it possible to achieve this without using Firebase Cloud Functions?
Yes but in this case you'll need to have your own server for sending message requests to the FCM backend, which will route messages to your client app running on users' devices. As you'll see in the doc this is possible for several languages, as well as by using the REST API.
I would like to use firebase messaging to schedule a message everyday,
at a specific time(say 9PM).
You'll have to schedule yourself the process of sending message requests to the FCM backend.
The advantage of using Cloud Functions is that you get all the needed components out of the box with a scheduled Cloud Function: A trusted server environment where your server logic runs with Cloud Function, and a scheduler to run this function as desired.
It will be like making ready-to-use things more complicated by re-inventing them. If you have millions or thousand of users only then it may be cost-effective to in-house this process.

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.

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

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