1

I am using firebase realtime database on node. I am using node clusters to fork different child processes. For each process, firebase events listener is initialized. But I want to listen to the firebase events only once in my application.

Multiple instance of node application initiates multiple observers which changes the integrity of data in firebase database as I perform certain updates on firebase db based on listeners. I, later would also be clustering node application on multiple aws instances.

My code looks something like this

if(cluster.isMaster){
  for (var i = 0; i < numWorkers; i++) {
    cluster.fork();
  }
}else{
  // initalize firebase event listeners
  firebaseEvents();
}

I would like to know how would I setup multiple node servers clustered together given that every server has internet connectivity and can listen to firebase events. But observer should run the code only once over all the servers.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

What you're trying to do is very close to the Firebase Queue, which uses Firebase Database transactions under the hood to ensure data consistency between a limited number of clients.

The README for Firebase Queue contains this message:

There may continue to be specific use-cases for firebase-queue, however if you're looking for a general purpose, scalable queueing system for Firebase then it is likely that building on top of Google Cloud Functions for Firebase is the ideal route.

I think the latter may apply to you too. Unless it is an academic exercise to get this system working, you'll likely find that Cloud Functions is an easier way to get an auto-scaling backend up and running.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you, Firebase cloud functions seems good but I also like to know if there also any alternate solutions. I am reluctant to deploy my entire code into cloud functions and hence being tied to their hosting service as well. In general, over a clusters of node application running same code, what would be the standard way to handle general events like firebase, emit events or even crons from running from more than once. I am pretty good with this solution but just curious about other ways to solve efficiently. – Sudhir Kumar Nov 28 '17 at 08:16
  • If you want to build this yourself, have a look at Firebase Queue (as said in the first part of my answer). It uses transactions to ensure data integrity between the clients/workers. – Frank van Puffelen Nov 28 '17 at 15:04