postwait / node-amqp

[UNMAINTAINED] node-amqp is an AMQP client for nodejs
MIT License
1.69k stars 357 forks source link

How to use direct exchange? #408

Open diegoaguilar opened 9 years ago

diegoaguilar commented 9 years ago

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?

pgasiorowski commented 9 years ago

Hi @diegoaguilar. Have you managed to figure this out?

diegoaguilar commented 9 years ago

@WooDzu kind of, however I'd appreciate your guide and comments

pgasiorowski commented 9 years ago

Well, I'm actually facing the same or similar issue. Have added extra connection.on('error', function (e) { console.log('AMQP', e.code, e.message); }); and what I'm getting is: AMQP 404 NOT_FOUND - no exchange 'my_messages' in vhost '/'

The only difference is that I create queue after exchange.