Firebase: responding to http POST request - firebase

I was thinking of import my server Java code to firebase, basically replace my Dynamic web app with Jeresy and mongoDB to Firebase using hosting and functions .
I have implemented the sendWelcomeEmail and sendByeEmail with events user.created and user.deleted.
But now I want to do something more complicated.
Based on post request that comes from the mobile I want to extract the JSON data and then update the DataBase.
So I created js file with plenty of functions but now I am not sure it will actually work.
Is that the best way to implement this workflow.
The workflow goes like this.
Image is taken on the android device => extract information from image ==> upload the Json Data to the server (Firebase Hosting) => functions is been execute is response to POST request ==> extarct the data ==> save it to the Firebase Database.
Let me know if this sound ok, or I need to implent another workflow.
Thanks
Eran

The whole idea of Firebase is that your app talks directly to backend services (such as the database, or cloud storage), and you only write server-side code (with cloud functions) for functionality that Firebase doesn't provide a client-side API for.
Why don't you simply let the Android client write directly to the Firebase Database?

Related

Should I make a RESTful API using Cloud Functions or call Firebase and Firestore in app?

I am currently creating a marketplace mobile application from scratch and I'm using React Native for the front-end part and Firebase for the backend (using Firebase and Firestore).
I am wondering wether :
I should create a RESTful API using cloud functions and express to create API endpoints (that would connect to firebase and firestore) that I'd call from my redux actions in my React Native app (using redux thunk).
or, I should call firebase and firestore directly from redux actions.
I see couple pros and cons for each method.
Restful API pros :
Safer as I can check and manipulate submitted data more easily
Custom API reply (I can manipulate the return data)
Restful API cons :
Probably slower as I would have to call an API endpoint that will then call firebase and/or firestore.
Won't be as easy to set listeners on firestore data
What do you think about it ?
Which option should I choose knowing that I predict that the app will get a few thousand users daily. I want it to be as fast and safe as possible and be able to set listeners for notifications purposes.
In my opinion you should mix them, if you have to manage users, products or etc. Firebase produces client and admin sdk that has different roles. If you haven't need manage any data of products or users or etc. you can simply use client sdk.
My advise is you can use admin sdk on API (server-side) and feel free to use client sdk on your clients.
Use as managements on API, listening data, realtime chat etc. via client sdk.
You can check admin and client sdk. Also npm packages for React Native here.
Mixing will be of help, you can try:
Listen for small amounts of data using the client SDK
Write and sort data using the cloud functions
In my experience, writing to a firebase database that uses ordered ids (in my case) leads to some huge problems in the long run. I lost a good chunk of data when my app accidentally wrote to a root index instead of a child index when the app was resumed from inactivity because the android system cleared the RAM.
Also, for notification, use Firebase Cloud Messaging.

Can we push data into firebase real time database using url query string?

i want push some data to my real time database firebase, I have my database structure like this
I've successfully done inserting data using query string with PHP using $_GET method in free web hosting, but it has limited hit request and i don't want to pay for web hosting, since I know firebase is free, I would like to use it.
ok so, i want to push the data using url query string. when i hit the url like
https://gps-iot-8a30e.firebaseio.com/koordinat?created_at=2020-01-01&latitude=6.231435&longitude=6.231454
the data should be added to 'koordinat' reference and automatically generate the key id. is there any way to insert data like that? What should i do? should I make some sort of REST API server? any help would be appreciated. thanks.
There is no way to add data to the Firebase Realtime Database REST API with a GET request. The new data will always have to be in the body of the request, which GET requests don't support.
See the Firebase documentation on ways to save data with the REST API.
What you can do is create a custom endpoint on for example Cloud Functions that takes the request you send from your PHP, and then converts that into a format that the Realtime Database supports. If you're doing this on Cloud Functions, you might as well use the Node.js SDK that Firebase provides, instead of dealing with the REST API there.

Retrieving data from firebase by the dialog flow inline editor fulfillment

I am just start learning how to write fulfillment to read a data stored in the firebase as a first step.
Actually, I stored the same data in the firebase realtime database
and the firebase cloud database just to try from which one i must read my data, so I am just stuck how to retrieve it by the inline editor
My data is just a JSON object String names.
Note : form where i should start to learn Node.js for dialogflow fulfillment to do basic operation as storing and retrieving ?
You can use the firebase-admin library for node.js.
The inline editor is just Firebase Cloud Functions under the covers.
Your fulfillment code needs to run "somewhere" in the cloud - you'll need an HTTPS URL that you will provide, so this is called a "webhook". Firebase Cloud Functions are a convenient place to do this - they provide a HTTPS endpoint and good scalability, but you can run your fulfillment webhook anywhere on the public Internet.
Dialogflow provides an easy way to use Firebase Cloud Functions, by providing the inline code editor. This uses Firebase Cloud Functions to do the work, but hides the URL from you, so it is one fewer thing you need to deal with.
There are a number of good places to get started, but one is using Google's Codelabs for the Assistant make sure you also have looked at Google Docs for Actions on Google, which links to other resources as well.

implement push notification in firebase with batch

I am trying to make firebase and batch work together so that I can fire push notification when something in firebase db got changed. I followed this tutorial and now i want to make them work automatically and for that i contacted with batch's and they replied me
write your own server-side code to 1. detect Firebase db changes 2. call the Batch API.
For "detect Firebase db changes" I have to create a server-side code which will track changes and call batch's api accordingly. Then follow this documentation for attaching the changed content and send it to appropriate user.
I don't know how to create the server side code. Anyone can give me a quick instructions on it or there's any tutorial related to this?
To write server-side Firebase code you can use the node.js SDK. See https://www.firebase.com/docs/web/quickstart.html (search the page for node.js).
The API is exactly the same as the regular web API except that you'll be writing it as a node.js application that can be run on the server. You can then integrate this with Batch's API.

Connecting Firebase Simple Login and External Server

How would you use Firebase's simple login to allow users to upload music files.
As I understand it, it doesn't make sense to even think about storing audio files in Firebase's database which is why I would like to be able to store them on an external PHP server.
So, the question revolves on whether I can use Firebase's simple login system to allow users to authenticate to an external server.
I have seen Using NodeJs with Firebase - Security ... which gives some great insight, but then how would you enable the large file upload to the external server?
The technique from the answer you linked will work for your situation too, you just need to translate it into PHP and the Firebase REST APIs. Additionally, since the REST API isn't real-time you must add some kind of task queue that it can poll.
Your program would flow something like this:
User logs in to Firebase with Simple Login
User write to only a place that they can (based on security rules). The user also writes an entry into a task queue.
Your PHP server connects with a token that allows reads of all of the user's secret places.
Your PHP server polls the firebase every once in awhile to look for new tasks. If there's a new task, it validates the user and allows that user to post data to it.
All that being said, this is going to be pretty complicated. PHP's execution model does not lend itself well to real-time systems, and
I strongly recommend you consider some other options:
You're using a cloud platform, Firebase, for your realtime stuff, so consider a cloud service for your binaries too, like filepicker.io
If you really want to host the files yourself, use something that's more real-time like node.js. It'll save you the effort of constructing that task queue.

Resources