Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Cloud Function For Firebase - GITS

215 views

Published on

Cloud Function For Firebase
Google Cloud Functions is Google's serverless compute solution for creating event-driven applications. It is a joint product between the Google Cloud Platform team and the Firebase team.

Published in: Technology
  • Login to see the comments

Cloud Function For Firebase - GITS

  1. 1. Cloud Function For Firebase GITS Indonesia
  2. 2. Sudaryatno Technical Lead GITS Indonesia
  3. 3. Serverless, function as service (Faas) Adalah cloud computing code execution model dimana cloud provider dapat memanage secara meyeluruh, menjalankan dan memberhentikan container dari setiam function. Dan harga yang harus dibayarkan adalah ekesuti dari function tersebut bukan per VM, per hit API, per Jam
  4. 4. Progression Serverless
  5. 5. Vendor Severless AWS Lamda Firebase Function Microsoft Azure IBM OpenWisk
  6. 6. Price Firebase Function
  7. 7. Intro Cloud Function adalah tempat hosting yang private dan scalable untuk Node.js environment. Firebase SDK sekarang sudah terintegrasi dengan Cloud Function yang dapat meresponse dan merequest fitur lain dari Firebase
  8. 8. Key capabilities ● Integrate The Firebase Platform ● Zero maintenance ● Keep your logic private and secure
  9. 9. Implement Path Set up Functions Write Functions Deploy & Monitor
  10. 10. Function Start npm install -g firebase-tools
  11. 11. myproject +- .firebaserc # Hidden file that helps you quickly switch between | # projects with `firebase use` +- firebase.json # Describes properties for your project +- functions/ # Directory containing all your functions code +- package.json # npm package file describing your Cloud Functions code +- index.js # main source file for your Cloud Functions code +- node_modules/ # directory where your dependencies (declared in # package.json) are installed
  12. 12. Perform Realtime Database sanitization and maintenance 1. Event user melakukan write di database realtime 2. Function membaca action dan juga dapat membrikan logic dari action tersebut 3. Function menulis kembali di database realtime dengan path yang sama atau berbeda
  13. 13. GITS CODE Perform Realtime Database sanitization and maintenance exports.makeUppercase = functions.database.ref('/messages/{pushId}/original').onWrite(event => { // Grab the current value of what was written to the Realtime Database. const original = event.data.val(); console.log('Uppercasing', event.params.pushId, original); const uppercase = original.toUpperCase(); // You must return a Promise when performing asynchronous tasks inside a Functions such as // writing to the Firebase Realtime Database. // Setting an "uppercase" sibling in the Realtime Database returns a Promise. return event.data.ref.parent.child('uppercase').set(uppercase);
  14. 14. Execute intensive task in the cloud instead of in your app 1. Function mendapatkan trigger dari user yang upload image 2. Function download image dan membuat versi thumbnail 3. Function menyimpan path thumbnail di database 4. Function upload ulang ke firebase storage
  15. 15. GITS CODE return mkdirp(tempLocalDir).then(() => { // Download file from bucket. const bucket = gcs.bucket(event.data.bucket); return bucket.file(filePath).download({ destination: tempLocalFile }).then(() => { console.log('The file has been downloaded to', tempLocalFile); // Generate a thumbnail using ImageMagick. return spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile]).then(() => { console.log('Thumbnail created at', tempLocalThumbFile); // Uploading the Thumbnail. return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath }).then(() => { console.log('Thumbnail uploaded to Storage at', thumbFilePath); }); }); }); });
  16. 16. Integrate with third-party service and APIs 1. Function dapat triger dari webhook github 2. Function memanggil api postMessage
  17. 17. GITS CODE exports.githubWebhook = functions.https.onRequest((req, res) => { const cipher = 'sha1'; const signature = req.headers['x-hub-signature']; // TODO: Configure the `github.secret` Google Cloud environment variables. const hmac = crypto.createHmac(cipher, functions.config().github.secret) // The JSON body is automatically parsed by Cloud Functions so we re-stringify it. .update(JSON.stringify(req.body, null, 0)) .digest('hex'); const expectedSignature = `${cipher}=${hmac}`; // Check that the body of the request has been signed with the GitHub Secret. if (signature === expectedSignature) { postToSlack(req.body.compare, req.body.commits.length, req.body.repository).then(() => { res.end(); }).catch(error => { console.error(error); res.status(500).send('Something went wrong while posting the message to Slack.'); }); } else { console.error('x-hub-signature', signature, 'did not match', expectedSignature); res.status(403).send('Your x-hub-signature's bad and you should feel bad!'); }});
  18. 18. Full Sample https://github.com/firebase/functions- samples
  19. 19. Google Cloud Functions is Google's serverless compute solution for creating event-driven applications. It is a joint product between the Google Cloud Platform team and the Firebase team.
  20. 20. Thanks! Contact us: Sudaryatno GITS Indonesia Bandung, Mars Barat 1 no 9 yatnosudar@gits.co.id www.gits.id

×