Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 219
nfpyfzyf Avatar asked Apr 22 '13 10:04

nfpyfzyf


1 Answers

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);
            });
    });
like image 53
Niels Avatar answered Sep 19 '22 16:09

Niels