EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 448 forks source link

Firebase (cloud) functions #541

Open d3mac123 opened 6 years ago

d3mac123 commented 6 years ago

Can we use the plugin with Firebase Cloud Functions? If so, any examples on how to use them?

dbeulen commented 6 years ago

The firebase cloud functions run in the cloud (on a google server), and have no direct connection to any client (not web, not android, not ios). See the cloud functions as a way to run code server side.

So to answer your question: no you cannot use this plugin to run cloud functions. That is simply not what cloud functions are for.

See the documentation of firebase for more information: https://firebase.google.com/docs/functions/

d3mac123 commented 6 years ago

Thanks for replying. I think I was not clear on my question. What I need is a way to call a Cloud Function (for example, something that migrates data from Real Database to FireStore) from inside my {N} app.

cerealexx commented 6 years ago

As @dbeulen said, it doesn't have anything to do with clients. You can build a Firebase function that listens (and triggers) to http requests done by your app. But as I said, nothing to do with this plugin.

BMwanza commented 5 years ago

Just hoping on this conversation now, so if I'm understanding correctly, No Firebase Cloud Function code is included in your {N} app. In other words the code in your {N} App doesn't even know that a Firebase Cloud Function exists.

However, when let's say you write somewhere in your Firebase Database, the Cloud Function will get triggered and perform some action?

cerealexx commented 5 years ago

@BMwanza If you configure that function to trigger on some kind of change, then yes. You can have triggers like onCreate, onUpdate, onDelete and so on. But you can also build http triggered functions to be called by url fetching. https://firebase.google.com/docs/functions/use-cases

BMwanza commented 5 years ago

Yeah I was looking into the Http triggered functions too. With that being said I have two questions then:

1) In the event of using a http Triggered Cloud Function, I could use the Nativescript Http Module (https://docs.nativescript.org/ns-framework-modules/http) to make an http request to the Cloud base function?

2) Are the Firebase Cloud Functions then able to send out a Cloud Message (Push Notification) to an individual device when they are triggered? I am asking this because basically what I'm looking for in my app is: If User A does some Action, User B will receive that action as a Push Notification

cerealexx commented 5 years ago

Yes and yes. Though you must be careful on how you handle http request functionality. With database listening is easier to control when and who triggers what, thanks to security rules. With http anyone who knows the url could be using it or spamming it. For that, Firebase also has an httpCallable functionality that can request for example auth uids before triggering.

BMwanza commented 5 years ago

Okay I see that's awesome, also given my implementation, Database listening would probably be the smartest way to do this.

Sorry I'm really new to this kind of development, so I have (hopefully) one last dumb question for you:

Where exactly do you Implement the Cloud Functions?

cerealexx commented 5 years ago

You can config your project with the Firebase cli. Which I'm guessing you will have already installed (npm i -g firebase-tools). Just type firebase init inside your project and go through the process selecting functions when you're asked for the services you want. A functions directory with an index.js (or ts) will be created. There you can write your functions and deploy them with firebase deploy --only functions or just firebase deploy if you want to deploy everything.

BMwanza commented 5 years ago

Oh sweet that is awesome!! Thank you man you have been so so so helpful! One more thing actually, so if I want to send a cloud Message to an individual user and their device using the Cloud Function, I no longer use This Plugins syntax but rather the web Syntax in the Google Firebase docs?

cerealexx commented 5 years ago

Admin SDK runs in Node.js. So you need to code in either Javascript or Typescript.