diegoaguilar
diegoaguilar

Reputation: 8376

How to properly use RabbitMQ direct exchange communication with Node.js driver?

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:

  1. Creating badanie-exchange exchange which happens to be direct type.
  2. Defining a queue which gets a binding to the previously defined exchange and define a routing key for it.
  3. Start listening on that queue.
  4. Simply connect to that queue and publish.

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

Answers (2)

Nikhil
Nikhil

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

ArnauOrriols
ArnauOrriols

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

Related Questions