node.jsrabbitmqamqp

AMQP Connection Closed certain time interval with node js


I tried out last 3 days with the below issue. Kindly help me to resolve the issue,

>Error: Unexpected close
at succeed (/usr/local/lib/node_modules/amqplib/lib/connection.js:259:13)
at onOpenOk (/usr/local/lib/node_modules/amqplib/lib/connection.js:241:5)
at /usr/local/lib/node_modules/amqplib/lib/connection.js:160:32
at /usr/local/lib/node_modules/amqplib/lib/connection.js:154:12
at Socket.recv (/usr/local/lib/node_modules/amqplib/lib/connection.js:480:12)
at Socket.g (events.js:180:16)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:407:10)
at emitReadable (_stream_readable.js:403:5)
at readableAddChunk (_stream_readable.js:165:9)

I am using amqplib + node js. Whenever i started the server i got the above error with a time interval. Maximum it will occurs at 5 mins interval.

amqplib = amqplib.connect('amqp://'+rabit_host).then(function(conn)
{
    amqpconnection = conn;
});

io.sockets.on('connection', function(client)
{   
    client.on('receivemsg', function(arg)
    {
        amqpconnection.createConfirmChannel().then(function(channelObjSuccess)
        {
            channelObjSuccess.assertQueue(queue_name,{durable:false,autoDelete:true});
            client.assignObj = channelObjSuccess;
            channelObjSuccess.consume(queue_name, function(msg)
            {
                var encodemsg = msg.content.toString();
                var json_msg = JSON.parse(encodemsg);                           
                client.emit('chatrecive',json_msg);
            }).then(function(){
                console.log("Receive Consiuume Close");
            }); 
        });
    });

client.on('loginentry', function(arg)
{
    amqpconnection.createConfirmChannel().then(function(channelObjSuccess) {
        channelObjSuccess.assertQueue(queue_name,{durable:false,autoDelete:true});
    });         
});

client.on('sendmsg', function(arg)
{   
    var payload_stringify = JSON.stringify(arg);            
    amqpconnection.createConfirmChannel().then(function(channelObjSuccess) {
    channelObjSuccess.assertQueue(queue_name,{durable:false,autoDelete:true});
    channelObjSuccess.sendToQueue(queue_name, new Buffer(payload_stringify), {},
        function(err, ok) 
        {
            if (err !== null)
                console.log('Message Send Failure! ');
            else
            {
                channelObjSuccess.close();
            }
        });
    });
}); 

client.on('disconnect', function()
{   
    try {
    console.log("AMPQ Connection Closed - Disconnect");
    if(typeof(client.assignObj)!=undefined)
    {
        client.assignObj.close();
    }   
    }
    catch (alreadyClosed) {
        console.log("RabbitMQ Connection Already Closed " + alreadyClosed.stackAtStateChange);
    }
    });         
});

server.listen(port);

Solution

  • I think i found the answer, might be the issue is with the heartbeat as option while connecting to the AMQP. Eg:

    url = "amqp://turtle.rmq.cloudamqp.com/bqftjxzn?heartbeat=45";