Search code examples
node.jsamazon-web-servicesfirebase-realtime-databasemessage-queuenode-cluster

Firebase event listening on multi server clustered node


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.


Solution

  • 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.