This is how we got a cloud function cron job' running for one of our projects using nodejs & firebase:
Requirement:
- Basic understanding of nodejs
- Basic understanding of accessing firebase admin/functions from nodejs (backend)
- Step-1 Init: Init your local firebase functions on your local machine - project folder. ( Ex: firebase-sample$ firebase init) - Just follow the appropriate selection steps - Cloud Function, then the default Firebase project on which you have to install (Screenshot below - you should already be logged in via firebase login )
- Step-2 Update index.js: Once firebase is successfully initialized, you will find index.js file in the functions folder. This will be the file where we would be writing all the magic... Let's add some basic init. (attached below file...)
- const functions = require('firebase-functions');
const admin = require('./node_modules/firebase-admin');
const serviceAccount = require("./serviceAccountKey.json");
- admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://your-firebase-project.firebaseio.com/"
});
- Step-3: Firebase Admin access: Login to your firebase console - to your project - settings - users and permissions - service accounts tab - generate new private key (download) - rename it to serviceAccountKey.json and place it in your local machine project folder.
- Step-4 Your daily-job tick: Below code:
- exports.daily_job = functions.pubsub.topic('daily-tick').onPublish((event) => {
sendNotifications();
return 0;
});
- Step-5 Firebase DB Update Code: In the sendNotifications() function you can write all your logic to access external API add to the add the same to firebase database... (not going in firebase-database update detail via nodejs... let me know if you need that as well). IMP Note: To access external API you should be on BLAZE plan (pay as you go plan) of the firebase.
- Step-6: test locally: now you can run and test the function locally by simply running: $ node index.js from your functions folder. (Make sure to explicitly call sendNotifications outside the daily-tick function ... to test locally, that function call can be commented out once we deploy to cloud function)
- Step-7 Deploy: Once satisfied... run $ firebase deploy --only functions . to place your functions code to gcloud. And voila! your cron job should be up and running... (Which you can log into gcloud console and check... )
Hope it helps? let me know if you get stuck at any step.... best wishes!
Some good article bookmarked links:
- Firebase schedule functions - link
- gcloud app engine cron job scheduling - link . (This way of scheduling via app engine and yaml file has been depreciated, this is only for your knowledge if you would like to know how it was done earlier....)
- gcloud cron.yaml - link. (only for knowledge base)
/// sample index.js file..
const functions = require('firebase-functions');
const admin = require('./node_modules/firebase-admin');
//const serviceAccount = require("./cmi-app-service-key.json");
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://cpdv-bible.firebaseio.com/"
});
//create database ref
var verseOfTheDayDBRef = admin.database().ref('/verseoftheday');
var bibleDBRef = admin.database().ref('/bible');
var topic_verse_of_day = 'topic_verse_of_day';
// Uncomment below line only when testing locally// sendPushNotification();
exports.daily_job = functions.pubsub.topic('daily-tick').onPublish((event) => {
console.log("This job will run every day");
sendPushNotification();
});
function sendPushNotification() {
console.log("Access external api's.... ");
console.log("Write your firebase database update code here.... ");
}