I am building a feature where user subscribe to app by making payment, it sets a value in cloud Firestore like {"subscribed": true}. I want to set the value false after one year. Can you anyone show me an example how to do it ?
There's no way you can directly use Firestore to schedule any task in the future. If you're not running any custom backend service, then you probably have to look into doing it with Cloud Tasks (Cloud Functions to trigger them).
You can create a cloud function to schedule the Task and one function to handle the task (this will include the logic to update the subscribed flag to false again.
Related
I have an app, where I have an admin and a customer.
Admins can invite users that are in a line.
Admins can configure maximum waiting time for which a customer should show up after he gets invited.
If he does not show up in specified period of time I need to update the database to set the status of a customer's ticket to "no-appearance" and send notification to a customer.
How can I do that using firebase cloud functions?
It looks like I cannot setTimeout because functions are limited to 540 seconds and it is not applicable in my case.
Looks like Pub-sub functions can only be created with a specific period which you have to determine statically
You can use Google Cloud Task to send a scheduled job to trigger cloud functions. Here an article about that: https://medium.com/swlh/event-driven-scheduled-jobs-using-gcp-cloud-tasks-e712c760ab95
The difference of a Pub/Sub is that you can send a task with a dynamic schedule and that task runs only once.
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.
I'm building an app that allows users to schedule social media posts. The date (and time) when the post is supposed to go live is stored (along with the content of the post) in a cloud firestore document. Is there a way to trigger the cloud function based on the date set by the user?
The built-in type to trigger scheduled functions sets the schedule at deploy time. There is no built-in trigger type to trigger at a specific dynamic time. But you can use Cloud Scheduler to dynamically schedule a callback to a Cloud Function.
Doug Stevenson wrote a good blog post about that, so I recommend checking that out: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).
There is nowadays also a built-in trigger type for this: enqueue functions with Cloud Tasks
I am working on an android project using java where we have a subscription for pro users. Right now I am using firestore as my database, is there any way that I can automatically change the pro attribute in user from true to false in a specific time automatically as the subscription time end? In my user collection's document, there are attributes is_a_pro and pro_availability_time which is in json (YYYY-MM-DD). Thank you
There is nothing built into Firestore to automatically change documents at a given time.
The most common solution is to schedule a Cloud Function, that runs periodically, and then check which subscriptions have expired.
An alternative to running them periodically is to schedule a Cloud Task for when each subscription expires. For an example of how to do that, see Doug's blog post How to schedule a Cloud Function to run in the future with Cloud Tasks.
I am working on a project that, currently, is 100% Firebase. Ideally, given I'm fully Firebase, I'd like to stay with Firebase for a next task which is updating some of the records based on external API calls once per day.
I'm currently using Firebase Functions for triggered events, not using it for API calls, everything that happens in the functions is after a user does something, and doesn't respond back to any clients (only responds back to the database for updates).
Is Firestore Cloud Functions a good place to run something like this that could call an external API and then update as necessary? I saw the scheduled functions that require the Blaze plan, have considered it but not sure if there's another approach that's better built for this task.
Cloud Functions that trigger on Firestore events probably aren't what you're looking for. Firestore triggers only fire when something in your Cloud Firestore database has changed. That means you need something that's writing to some document in the database in order to get the code to run. Which means you need a way to schedule that operation.
No matter what kind of trigger you write, you will need to be on a billing plan in order to make external requests anyway. So even if you somehow managed to put together a solution that uses Firestore triggers, your project would still need to be on a billing plan.
This approach is perfectly okay - in fact, I am using the exact same approach in my project which has 100% Firebase back-end. The overall (Firebase) Cloud Functions gives flexibility in terms of invocation i.e. they can be invoked based on trigger (e.g. storage or database event) or can be called with the HTTP end-point. So, depending on your need you can either use Firestore trigger or database trigger or call an end-point.
Switching to Blaze plan is perfectly fine since otherwise we can't call an external end-point. I switched to Blaze plan just a few months back and didn't pay anything for that as my usage is within the free limit.