DEV Community

Cover image for How to Create Cron Jobs in Firebase Functions Using Pub/Sub (Free Tier Compatible)
HexShift
HexShift

Posted on

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

🚀 Alternatives

  • GitHub Actions: Free scheduled workflow runner
  • Upstash Scheduler: Cron HTTP calls to Firebase HTTPS functions
  • Zapier or Make: Non-code scheduling for simpler workflows

Conclusion

Firebase Functions don’t include native cron scheduling, but with Cloud Scheduler and Pub/Sub, you can set up powerful, scalable cron jobs with ease. It’s a powerful setup that plays nicely with Firebase’s free and flexible infrastructure.

If this article helped, you can support more like it at: buymeacoffee.com/hexshift

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more