Reputation: 8376
I'm trying a RabbitMQ first test with Node.js connection module, I wanted to try first direct exchange so I got both a server where I'm also trying to listen to messages and a publisher script.
Server:
var amqp = require('amqp');
var connection = amqp.createConnection();
connection.on('ready', function () {
var queue = connection.queue('mongo-ops');
connection.exchange('badanie-exchange', {type: 'direct', durable: 'true'}, function () {
queue.bind('badanie-exchange', '*');
queue.subscribe(function (message) {
console.log(message);
})
});
});
Publisher:
var amqp = require('amqp');
var connection = amqp.createConnection();
connection.on('ready', function () {
var exchange = connection.exchange('badanie-exchange');
exchange.publish('*', "La decima", function (err,result) {
console.log(err,result);
});
});
So, from my understanding I'm:
badanie-exchange
exchange which happens to be direct
type.However when I run both scripts, nothing happens on console. What am I doing wrong?
Testing from rabbitmqctl
I tried:
> $ sudo rabbitmqctl list_exchanges ⬡ 0.12.7
Listing exchanges ...
amq.direct direct
amq.topic topic
amq.rabbitmq.trace topic
amq.rabbitmq.log topic
amq.fanout fanout
amq.headers headers
direct
amq.match headers
badanie-exchange direct
...done.
And
> $ sudo rabbitmqctl list_queues ⬡ 0.12.7
Listing queues ...
mongo-ops 0
...done.
Upvotes: 0
Views: 6114
Reputation: 374
Probably it's an order that you are messing with. Try running the server.js file first then publisher.js. If you runs publisher.js first then it will not create a working queue and nothing will display on console.
Upvotes: 0
Reputation: 584
There are multiple minor issues with your code, for which I suggest you to read more closely the library documentation. However, there is one particular protocol issue that is preventing your code from working at all: if the exchange being declarated already exists, it must be declared with the same options as the existing exchange, otherwise a channel-error is raised and the channel is closed. From the AMQP specification:
If the exchange exists, the server MUST check that the existing exchange has the same values for type, durable, and arguments fields. The server MUST respond with Declare-Ok if the requested exchange matches these fields, and MUST raise a channel exception if not.
Getting more specific, when you publish the message, you must declare the exchange with the same options that the server use:
var amqp = require('amqp');
var connection = amqp.createConnection();
connection.on('ready', function () {
var exchange = connection.exchange('badanie-exchange',
{type: 'direct', durable: 'true'});
exchange.publish('*', "La decima", function (err,result) {
console.log(err,result);
});
});
Upvotes: 2