Search code examples
node.jsrabbitmqamqpnode-amqp

node-amqp cannot send message to RabbitMQ


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.


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);
                });
        });