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

Viewed 695

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.

2 Answers

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.

Related