0
votes

We are trying to build a chat application with nodejs , rabbitmq and mongodb. For faster delivery of messages , we are using rabbitmq . Since I am new to this concept, if anybody can, kindly help me out. I am using the node amqp module to connect to the rabbitmq server(https://www.npmjs.org/package/amqp) .

I am declaring an exchange as given below :

var amqp = require('amqp');var amqpconnection = amqp.createConnection({host: 'localhost'});var exchange = amqpconnection.exchange('Exchange',{confirm:true,type:"direct", durable:true, autoDelete:false},function (exchange){});

I am publishing a message using the below method :

exchange.publish('receiver_queue', message ,{persistent: true,mandatory:true},function(data){console.log("Message Published");console.log(data);});

My question is , when I am publishing to a queue that already exists , in the publish callback , we get a 'false' response but when I am publishing to a queue that doesn't exist at all , in the publish callback , still we get a 'false' response ; Why is it so ? Is there any method by which I can check whether a queue exists before publishing to it ?

2

2 Answers

1
votes

This can be considered an anti pattern, ie: most messaging applications use a fire and forget style, where the publisher doesn't care about who receives the message.

Now, if you need to make sure the message gets routed to some queue, then you can use the mandatory flag when publishing the message.

To know if a queue exists, you can use queue_declare, but passing the passive = true flag when sending the queue.declare

0
votes

Try this code.

connection.exchange('Exchange', {type: 'topic', confirm: true,passive:true},function(exchange) {
publish = exchange.publish(Queue, xml, { mandatory: false });
return true;

});