0

I am writing one node js service which receives messages using rabbitmq. But I am facing one issue when I am trying to send concurrent requests to my node js service.

Here is amqp subscriber I have written,

const amqp = require('amqplib/callback_api')
let AmqpConnection = {
  // some other methods to make connection
  // ....
  //....



  subscribe: function(){
    this.withChannel((channel) => {
      let defaultQueueName = "my_queue";

      channel.assertQueue(defaultQueueName, { durable: true }, function(err, _ok) {
        if (err) throw err;
        channel.consume(defaultQueueName, AmqpConnection.processMessage);
        Logger.info("Waiting for requests..");
      });
    })       
  },

  processMessage: function(payload){
    debugger
    try {
      Logger.info("received"+(payload.content.toString()))
    }
    catch(error){
      Logger.error("ERROR: "+ error.message)
      //Channel.ack(payload)
    }
  }
}

And now I am trying to publish messages to this using publisher,

const amqp = require('amqplib/callback_api')
let Publisher = {
  // some other methods to make connection
  // ....
  // ....

  sendMessage: function(message){

    this.withChannel((channel) => {

      let exchangeName = 'exchange';
      let exchangeType = 'fanout';
      let defaultQueueName = 'my_queue';
      channel.assertExchange(exchangeName, exchangeType)
      channel.publish(exchangeName, defaultQueueName, new Buffer(message));
    })
  }
}

let invalidMsg = JSON.stringify({ "content": ""})
let correctMsg = JSON.stringify({ "content": "Test message"})


setTimeout(function () {
  for(let i=0; i<2; i++){
    Publisher.sendMessage(correctMsg)
    Publisher.sendMessage(invalidMsg)
  }
}, 3000)

But when I execute both publisher and subscriber, I get following output on subscriber side

2017-02-18T11:27:55.368Z - info: received{"content":""}
2017-02-18T11:27:55.378Z - info: received{"content":""}
2017-02-18T11:27:55.379Z - info: received{"content":""}
2017-02-18T11:27:55.380Z - info: received{"content":""}

It seems like concurrent requests are overriding message received. Can someone help here?

Pandurang Waghulde
  • 995
  • 1
  • 6
  • 19

0 Answers0