Question

I'm tring rabbitmq-tutorials, ruby version works fine, but node.js version cannot send message. I do not know what is wrong.

var amqp       = require('amqp');
var amqp_hacks = require('./amqp-hacks');

var connection = amqp.createConnection({host: 'localhost'});

connection.on('ready', function(){
    connection.publish('hello_node', 'Hello World!');
    console.log(" [x] Sent 'Hello World!'");

    amqp_hacks.safeEndConnection(connection);
});

after I run node send.js, runing process node recv.js cannot recv anything. and rabbitmqctl list_queues does not show hello_node queues.

Was it helpful?

Solution

You need to indicate the queue then publish. That version should works:

    var amqp       = require('amqp');
    var amqp_hacks = require('./amqp-hacks');

    var connection = amqp.createConnection({host: 'localhost'});

    connection.on('ready', function(){
            connection.queue('hello_node', {'durable': false}, function(q){
                connection.publish('hello_node', 'Hello World!');
                console.log(" [x] Sent 'Hello World!' to 'hello_node'");

                amqp_hacks.safeEndConnection(connection);
            });
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top