Any help or guidance would be appreciated.

24 views
Skip to first unread message

Victor Unda

unread,
Sep 3, 2019, 4:49:24 PM9/3/19
to Flutter Development (flutter-dev)
Any help or guidance would be appreciated. 

I would like to write a script in google cloud function to get data from an outside server and push inside of the google cloud firebase every 24 hours or more. 

Does anyone have any ideas or where to go to explore this. I am not looking to upload the data into the firebase. I am looking to write or find somebody who has written a script to GET that information and insert it into google cloud Firebase.

Again, thank you for your help,

Best,


EthicCoders Apps

unread,
Sep 3, 2019, 9:11:02 PM9/3/19
to Victor Unda, Flutter Development (flutter-dev)
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... runfirebase 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.... ");
}


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/feda0065-9659-4ecc-95a0-0ab08c97bb54%40googlegroups.com.

Wayne Werner

unread,
Sep 4, 2019, 7:27:10 PM9/4/19
to Flutter Development (flutter-dev)
Reply all
Reply to author
Forward
0 new messages