0
votes

I am getting duplicate MessageIDs on Google Cloud Pub / Sub. Do note that my payload aka the data field is the same during testing but this should ideally not result in duplicate MessageIDs. Please let me know what's wrong with my code below:

const publish = (topicName, payload) => {

    const dataBuffer = Buffer.from(JSON.stringify(payload));

    return new Promise((resolve, reject) => {

        pubsub
        .topic(topicName)
        .publisher()
        .publish(dataBuffer)
        .then(result => {
            const messageId = result[0];
            console.log(`${messageId} published`);
            resolve(messageId);
        })
        .catch(err => {
            console.error(err);
            reject(err);
        });

    });

};
1

1 Answers

3
votes

Your code conflates the publish call available in two different libraries. The publish call you are using comes from the higher-performance Publisher library. In this library, you do not get back an array of results; you get back a single message ID. Therefore, when you do messageId = result[0], you are getting the first character of the messageID. If you were to just print out all of result, you'd see that they are different for each publish call.

The array of results comes from the PublisherClient publish method. This method takes a raw PublishRequest and returns a list of responses, which is when you need to index the responses you get.