1

I am using node AMQP module to connect to RabbitMQ. I am able to connect, create an exchange, queue and able to send/publish the message to the exchange. I can confirm the messages are published on the management console.

Problem is I am not receiving the callback for exchange publish call. This is my code.

Initialization: (app is the express.js instance)

                app.rabbitMQConnection = amqp.createConnection({ host: 'myurl.com', login: 'login', password: 'pwd' });
                app.rabbitMQConnection.on('ready', function(){
                    console.log("RabbitMQ server connected");
                    app.rabbitMQConnection_e = app.rabbitMQConnection.exchange('my-exchange', { confirm: true, durable: true, autoDelete: false }, function (q) {
                        app.rabbitMQConnection_q_lisorders = app.rabbitMQConnection.queue('shoe-orders', {autoDelete: false, durable: true}, function (q) {
                            app.rabbitMQConnection_q_lisorders.bind(app.rabbitMQConnection_e, '#');
                        });
                    });
                });

Then when I need to send a message I use:

                    app.rabbitMQConnection_e.publish('routingKey', { message: myMessage }, {  deliveryMode: 2 }, function(transmissionFailed){
                            if (transmissionFailed == true){ 
                                        console.log("message failed");
                            }else{
                                        console.log("message sent");
                            }
                    });

Callback function(transmissionFailed) is never called. Please help!

user3658423
  • 1,904
  • 5
  • 29
  • 49

2 Answers2

0

Doc is not clear enough. As we use it,

when calling: connection.exchange(name, options={}, openCallback)

Then callback has exchange object which is used to publish messages.

Your code would be:

app.rabbitMQConnection.exchange('my-exchange', { confirm: true, durable: true, autoDelete: false }, function (q) {
    app.rabbitMQConnection_e = q;
    .....
});

And then:

app.rabbitMQConnection_e.publish('routingKey', { message: myMessage }, {  deliveryMode: 2 }, function(transmissionFailed){
                        if (transmissionFailed == true){ 
                                    console.log("message failed");
                        }else{
                                    console.log("message sent");
                        }
                });
Eguzki
  • 1
  • 3
0

callback is a function that will get called if the exchange is in confirm mode, the value sent will be true or false, this is the presense of a error so true, means an error occured and false, means the publish was successfull

so we should set the exchange as confirm mode when create an exchange

hww
  • 11
  • 2