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

Image of Datadog

Diagram Like A Pro

Bring your cloud architecture to life with expert tips from AWS and Datadog. In this ebook, AWS Solutions Architects Jason Mimick and James Wenzel reveal pro strategies for building clear, compelling diagrams that make an impact.

Get the Guide

Top comments (0)

Image of Datadog

Get the real story behind DevSecOps

Explore data from thousands of apps to uncover how container image size, deployment frequency, and runtime context affect real-world security. Discover seven key insights that can help you build and ship more secure software.

Read the Report

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay