bpinhosilva
bpinhosilva

Reputation: 704

What causes amqp.node to get ECONNRESET from a RabbitMQ server?

I have an instance of RabbitMQ (default configurations) running on Windows 8 environment. My node version is 5.1.0 and I am trying to establish a connection between them using amqp.node library in order to pass messages.

When I try the sample code:

var q = 'tasks';

function bail(err) {
  console.error(err);
  process.exit(1);
}

// Publisher
function publisher(conn) {
  conn.createChannel(on_open);
  function on_open(err, ch) {
    if (err != null) bail(err);
    ch.assertQueue(q);
    ch.sendToQueue(q, new Buffer('something to do'));
  }
}

// Consumer
function consumer(conn) {
  var ok = conn.createChannel(on_open);
  function on_open(err, ch) {
    if (err != null) bail(err);
    ch.assertQueue(q);
    ch.consume(q, function(msg) {
      if (msg !== null) {
        console.log(msg.content.toString());
        ch.ack(msg);
      }
    });
  }
}

require('amqplib/callback_api')
  .connect('amqp://guest:guest@localhost:5672', function(err, conn) {
    if (err != null) bail(err);
    consumer(conn);
    publisher(conn);        
  });

I get it working okay.

But if I move this code, exactly how it is, into my project, I get this error:

ECCONRESET syscall: read.

My app runs with express.js and oauth2.0. Even if I put the code right before any other statements and modules' requires, it does not work.

I searched about this error and I found some problems related to load balance, but I am running it locally and the sample code works okay.

Another problem that I found could be related to TCP connection, I changed the handshake timout option inside RabbitMQ server's config file to 10000ms, but nothing changed.

I am using the same url with guest user: amqp://guest:guest@localhost:5672, which works on the sample code.

The log from RabbitMQ shows that a connection is done but a few seconds later, it states that the connection was closed unexpectedly:

=INFO REPORT==== 24-Nov-2015::18:09:52 ===
accepting AMQP connection <0.2644.0> (127.0.0.1:51866 -> 127.0.0.1:5672)

=ERROR REPORT==== 24-Nov-2015::18:10:12 ===
closing AMQP connection <0.2644.0> (127.0.0.1:51866 -> 127.0.0.1:5672):
{handshake_timeout,frame_header}

So my questions are: is there any conflict between amqp.node and other library that drops the connection with the server? How can I debug this?

Upvotes: 6

Views: 14860

Answers (2)

Sukhjeet Singh Mangat
Sukhjeet Singh Mangat

Reputation: 451

This error is related to connection and channels. So in your code you are creating a connection and channel but not closing them. That creates this error (ECONNRESET) when the no. of connection and channel limit is exhausted RabbitMQ will stop accepting new network connections. Closing the channel and connection will solve this error. Example Code:

amqp.connect('amqp://localhost')
.then(function(conn) {
    return when(conn.createChannel().then(function(ch) {
        var q = 'hello';
        var msg = 'Hello World!';

        var ok = ch.assertQueue(q, {durable: true});

        return ok.then(function(_qok) {
            ch.sendToQueue(q, new Buffer(msg), {deliveryMode: true});
            console.log(" [x] Sent '%s'", msg);
            return ch.close();
        });
    })).ensure(function() {
        conn.close();
    });
})
.then(null, console.warn);

Upvotes: 10

Derick Bailey
Derick Bailey

Reputation: 72888

ECONNRESET is typically a network error, meaning it was forcefully disconnected from the TCP/IP connection it tried to make.

It's possible that your project is running in a manner that prevents it from opening the TCP/IP connection properly, or hits a firewall, or something else.

I've also seen this error when the username and password are bad. I would highly recommend you set up a new username and password, and grant that user the correct permissions to the vhost in question (the default vhost in this case).

beyond that... it sometimes takes some playing with the code to figure out what's causing the problem. if you're not seeing this problem when you copy & paste into your real project, there may be something else going on in your project that is preventing it from working.

...

P.S. I'd recommend not using amqplib directly. It's a great driver, but there are better (friendlier) API layers on top of it to make life easier. For example, my favorite is wascally which uses amqplib under the hood. I've recorded screencasts about all this, at http://RabbitMQ4Devs.com

Upvotes: 4

Related Questions