postwait / node-amqp

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

AMQP throwing an error - connection_closed_abruptly #406

Open ghost opened 8 years ago

ghost commented 8 years ago

Hello,

I am having a code written in node.js as below:

//in server file. exports AMQP library amqp = require('amqp'); //expoerts amqp class file here. amqpClass = module.exports = require("./classes/amqp.class.js");

//connect rebbitMQ
rabbitConn = module.exports = amqp.createConnection({host: "xxx.xxx.xxx.xxx", login: "user_name",password: "password",vhost:"beta",heartbeat : 60});

rabbitConn.on('error', function (e) { console.log(e); });

//Server ready to connect with rebbitMQ rabbitConn.on('ready', function () { console.log("AMQP Connected: "); //here we have create exchange with "topic" type. playExchange = module.exports = rabbitConn.exchange('playExchange', {'type': 'topic'});
//Here we have create queues. call amqpClass. amqpClass.CreateQueues(); });

AND, here is the part of amqp.class.js file code:

CreateQueues : function(){ //queue name tables-s1_1 is one server //s1_1, s1_2 ..........s1_10 servers use all server publish queue. rabbitConn.queue('tables-s1_1, { exclusive: false, arguments : { "x-message-ttl" : 10000 } }, function (q) { q.bind(playExchange,"table.*"); q.subscribe(function(message, headers, deliveryInfo, messageObject){ if(message == null) return false;
if(message.en == 'CTJ') { schedule.cancelJob(message.data.jid); return false; }

            //first we have to get rable id from routing key.
            var room = deliveryInfo.routingKey.replace('table.','');

            //room must be exists in routing key.
            if(typeof room == 'undefined' || room == null  || room == 'undefined')
                return false;

            var eData = commonClass.Enc(message);
            if(typeof io.to(room) != 'undefined');
            io.to(room).emit('res', eData);

            if((message.en == 'LT' && message.auto == 1) || (message.en == 'TLT' && message.noChips == 1))
            {
                rClient.get('user:'+message.UserId,function(err,sockid){ 
                    if(io.sockets.connected[sockid])
                        io.sockets.connected[sockid].leave(room); 
                                                  //leaving the table by socket id instance.
                });
            }
        });
    });

}

AND, here is the event/function called from other node.js servers:

playExchange.publish('table.' + tbId, {en: 'NTF', data: {m: winner.cht, k: 'WINNER'}});

The problem is that we get the RabbitMQ server disconnected from Node.js server. Following is the log sequence on Node.js server:

[TypeError: Cannot read property 'cancel' of null](In the error handler function on this node server) AMQP Connected: (in the ready handler function on this node server)

Here is the log sequence on RabbitMQ server:

=WARNING REPORT==== 2-Oct-2015::05:49:24 === closing AMQP connection <0.15333.2> (54.86.37.136:60279 -> 172.31.25.112:5672): connection_closed_abruptly

=INFO REPORT==== 2-Oct-2015::05:49:25 === accepting AMQP connection <0.15628.2> (54.86.37.136:60293 -> 172.31.25.112:5672)

I can not understand what is the reason behind the Node.js server getting disconnected from RabbitMQ server and why do I get error saying "Cannot read property 'cancel' of null" on Node.js server. Also it reconnects automatically to RabbitMQ.

Looking forward to some thoughts/help on this. Thank you for looking into this :)

Thanks, Naresh