How to Create Cron Jobs in Firebase Functions Using Pub/Sub (Free Tier Compatible)
If you're building on Firebase, you might wonder how to implement scheduled (cron) jobs in a serverless setup. Firebase doesn’t have built-in cron support, but with Google Cloud Pub/Sub and Cloud Scheduler (with Firebase Functions), you can build reliable cron jobs — and best of all, you can do it within the free tier if you're careful. Why Use Cloud Scheduler with Firebase Functions? Fully serverless and scalable Low-maintenance once configured Compatible with the Firebase free tier for many use cases Step 1: Enable Required APIs In the Google Cloud Console linked to your Firebase project, enable: Cloud Scheduler API Cloud Pub/Sub API Step 2: Create the Pub/Sub Topic gcloud pubsub topics create my-scheduled-task Step 3: Deploy a Firebase Function that Listens to the Topic In your functions/index.js (or .ts): const functions = require('firebase-functions'); exports.scheduledFunction = functions.pubsub .topic('my-scheduled-task') .onPublish((message) => { console.log('Running scheduled job:', new Date().toISOString()); // Your logic here, e.g. data cleanup return Promise.resolve(); }); Step 4: Create a Cloud Scheduler Job gcloud scheduler jobs create pubsub run-my-scheduled-task \ --schedule="*/15 * * * *" \ --time-zone="UTC" \ --topic=my-scheduled-task \ --message-body="{}" This runs your function every 15 minutes. Modify the schedule string as needed. Pros and Cons ✅ Pros Integrates seamlessly with Firebase Uses managed services, very low maintenance Highly scalable and robust ⚠️ Cons Initial setup is more complex than other solutions May incur cost if usage exceeds free tier (e.g. high-frequency tasks) Limited visibility into execution logs unless using full GCP console
If you're building on Firebase, you might wonder how to implement scheduled (cron) jobs in a serverless setup. Firebase doesn’t have built-in cron support, but with Google Cloud Pub/Sub and Cloud Scheduler (with Firebase Functions), you can build reliable cron jobs — and best of all, you can do it within the free tier if you're careful.
Why Use Cloud Scheduler with Firebase Functions?
- Fully serverless and scalable
- Low-maintenance once configured
- Compatible with the Firebase free tier for many use cases
Step 1: Enable Required APIs
In the Google Cloud Console linked to your Firebase project, enable:
- Cloud Scheduler API
- Cloud Pub/Sub API
Step 2: Create the Pub/Sub Topic
gcloud pubsub topics create my-scheduled-task
Step 3: Deploy a Firebase Function that Listens to the Topic
In your functions/index.js
(or .ts
):
const functions = require('firebase-functions');
exports.scheduledFunction = functions.pubsub
.topic('my-scheduled-task')
.onPublish((message) => {
console.log('Running scheduled job:', new Date().toISOString());
// Your logic here, e.g. data cleanup
return Promise.resolve();
});
Step 4: Create a Cloud Scheduler Job
gcloud scheduler jobs create pubsub run-my-scheduled-task \
--schedule="*/15 * * * *" \
--time-zone="UTC" \
--topic=my-scheduled-task \
--message-body="{}"
This runs your function every 15 minutes. Modify the schedule string as needed.
Pros and Cons
✅ Pros
- Integrates seamlessly with Firebase
- Uses managed services, very low maintenance
- Highly scalable and robust
⚠️ Cons
- Initial setup is more complex than other solutions
- May incur cost if usage exceeds free tier (e.g. high-frequency tasks)
- Limited visibility into execution logs unless using full GCP console