0

I need my onPublish method (firebase side) to get only one message at time.

My code is like this:

Google Cloud (AppEngine):

const pubsub = new PubSub({
  projectId: config.project,
});

const topicName = 'projects/'+config.project+'/topics/user_assigner';

const dataBuffer = Buffer.from(dataToSend);
  pubsub
    .topic(topicName)
    .publisher()
    .publish(dataBuffer, (err, messageId)=> {
      console.log("call back")
      console.log("err",err)
      console.log("messageId",messageId)
      resolve(messageId)
    })

Firebase

    var functions = require('firebase-functions');

    module.exports =
      functions.pubsub.topic('user_assigner').onPublish((event) => { 
          return someAsyncCode() //return promise;
      });

I need the code someAsyncCode to run messages one by one, is there a way to do that? I have seen on the documentation of the publisher the batch maxMessages but I think that is to set the limit when the publisher push new messages to the queue of messages. So, if I set that attibute to one is the same, the publisher will push one buy one(gcloud side), and the onPublish (firebase side) will get all the messages no matter if it is processing one.

Guillermo Cacheda
  • 2,162
  • 14
  • 23
franmemo
  • 16
  • 1
  • 3
  • I don't understand what you're trying to accomplish. Are you sure pubsub is the right solution for whatever it is you want to do? – Doug Stevenson Jul 30 '18 at 19:03
  • The maxMessages property is ["The maximum number of messages to buffer before sending a payload"](https://cloud.google.com/nodejs/docs/reference/pubsub/0.19.x/Publisher#Publisher). I don't know how will it interact with Firebase but it could help on what you are trying to accomplish. – Guillermo Cacheda Jul 31 '18 at 14:06
  • The maxMessages property will have no impact on the number of messages received at a time by the subscriber. It is strictly related to optimizing throughput and latency on the publisher. When you say you only want only one message at a time, do you mean that you want a message to be processed completely before you receive the next message? If so, is there something inherent in what you are doing that requires that behavior? It would generally be considered an anti-pattern in Pub/Sub. – Kamal Aboul-Hosn Aug 02 '18 at 15:22

0 Answers0