Nodejs-pubsub: Messages are received only sometimes

Created on 25 May 2018  路  10Comments  路  Source: googleapis/nodejs-pubsub

Environment details

  • OS: Ubuntu
  • Node.js version: 10.2.0
  • npm version: 5.6.0
  • @google-cloud/pubsub version: 0.18.0

Steps to reproduce

I have a topic named test with a subscribtion alos named test. I created a simple test.js file that looks like this:

const PubSub = require(`@google-cloud/pubsub`);
const pubsub = new PubSub();
const topicName = 'test';
const subscription = pubsub.subscription(topicName);


const messageHandler = (message) => {
    try {
        let data = JSON.parse(message.data.toString());
        console.log(data);
        message.ack();
    } catch (err) {
        console.log(err);
    }
}

subscription.on('message', messageHandler);

setTimeout(() => {
    subscription.removeListener('message', messageHandler);
}, 5 * 1000);

const dataBuffer = Buffer.from(JSON.stringify({name: 'test', message: 'this was a test'}));
pubsub.topic(topicName).publisher().publish(dataBuffer);

The problem I have is after running this 100 times, only about half of the times the message was logged to the console. There is no repeated sequence to it, sometimes I receive 3 in a row, then 5 times I don't...
I tried increasing the timeout, also removed it. I have no idea what else to do.
Am I missing something or is this a pubsub issue?

pubsub p1 bug

Most helpful comment

Hello @callmehiphop.
Sorry for the late response.
Since I couldn't resolve this issue at the time I switched to rabbitMQ.
However I started a new project recently where I also need some kind of message queue software and I decided to try PubSub again and so far I haven't had any problems.
I will just close this issue for now and reopen it If anything were to change.

All 10 comments

After creating a new topic and a new subscription it started to work everytime..
Not sure what was the problem.

Hi @mirthlaj have you experienced the same behavior again or all running smoothly now?

@danoscarmike
I restarted testing after reading your question and the same thing started happening again. Its like after a while it starts eating messages.
Have there been any similar issues?

@stephenplusplus can you please take a look?

@mirthlaj is it possible that the message is taking longer than expected to be delivered? e.g. if you do not remove the message handler does the message eventually get delivered?

Also have you tried chaining off of the publish call to make sure the message was published successfully?

const topic = pubsub.topic(topicName);
const publisher = topic.publisher();

publisher
  .publish(Buffer.from('hi'))
  .catch(err => console.error(err));

@callmehiphop Yes I tried both but the result is still the same.

@callmehiphop you're probably better off handling this, given your understanding of the wires that run through this library. Feel free to pass it back if you have any leads I can follow.

@mirthlaj its been kind of a while, but is this still an issue for you? If so, would you mind testing against the latest version (v0.23.0)?

Hello @callmehiphop.
Sorry for the late response.
Since I couldn't resolve this issue at the time I switched to rabbitMQ.
However I started a new project recently where I also need some kind of message queue software and I decided to try PubSub again and so far I haven't had any problems.
I will just close this issue for now and reopen it If anything were to change.

@mirthlaj thanks for the update!

Was this page helpful?
0 / 5 - 0 ratings