Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Connection failing on connecting to RabbitMQ

I have a node.js app to be running with rabbitmq and mysql. After setting everything up, when I do npm start

I have given the user administrator tag and permissions as rabbitmqctl set_permissions RABBIT_USERNAME "." "." ".*"

It shows this error :

> node app.js

13:43   
Started database log (Mon Jan 29 2018 13:43:42 GMT+0530 (IST))
13:43   
Started mailer log (Mon Jan 29 2018 13:43:44 GMT+0530 (IST))
13:43   
Started messaging log (Mon Jan 29 2018 13:43:44 GMT+0530 (IST))
13:43   
Started messaging log (Mon Jan 29 2018 13:43:44 GMT+0530 (IST))
13:43   
Started database log (Mon Jan 29 2018 13:43:44 GMT+0530 (IST))
13:43   
Started socket log (Mon Jan 29 2018 13:43:44 GMT+0530 (IST))
13:43   messaging:3000 is listening..
13:43   DB connected (7)
13:43   DB connected (6)
13:43   Error:



 Expected ConnectionOpenOk; got <ConnectionClose channel:0>
 Error: Expected ConnectionOpenOk; got <ConnectionClose channel:0>
    at /PATH/node_modules/amqplib/lib/connection.js:167:14
    at /PATH/node_modules/amqplib/lib/connection.js:159:12
    at Socket.recv (/PATH/node_modules/amqplib/lib/connection.js:497:12)
    at Object.onceWrapper (events.js:255:19)
    at Socket.emit (events.js:160:13)
    at emitReadable_ (_stream_readable.js:520:10)
    at emitReadable (_stream_readable.js:514:7)
    at addChunk (_stream_readable.js:280:7)
    at readableAddChunk (_stream_readable.js:256:11)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onread (net.js:599:20)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! messaging@ start: `node app.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the messaging@ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/mishal23/.npm/_logs/2018-01-29T08_13_45_095Z-debug.log
like image 560
Mishal Shah Avatar asked Jan 29 '18 08:01

Mishal Shah


People also ask

Can't connect to RabbitMQ?

Make sure the node is running using rabbitmq-diagnostics status. Verify config file is correctly placed and has correct syntax/structure. Inspect listeners using rabbitmq-diagnostics listeners or the listeners section in rabbitmq-diagnostics status. Inspect effective configuration using rabbitmq-diagnostics environment.

What ports need to be open for RabbitMQ?

To connect to RabbitMQ from a different machine, you must open ports 5672 and 5672 for remote access.

How do I add connections to RabbitMQ?

In order for a client to interact with RabbitMQ it must first open a connection. This process involves a number of steps: Application configures the client library it uses to use a certain connection endpoint (e.g. hostname and port) The library resolves the hostname to one or more IP addresses.


2 Answers

There is no enough context on how this happened so I'm making a few assumptions:

  1. RabbitMQ is not installed locally and it is installed on some server.
  2. You're using amqplib for the connection. (Not an assumption)
  3. You haven't created any Virtual Hosts.

Setting Up

So the first thing you need to do is to create a user if you haven't already.

sudo rabbitmqctl add_user admin password
sudo rabbitmqctl set_user_tags admin administrator

Run this command if you haven't enabled the Management Dashboard

sudo rabbitmq-plugins enable rabbitmq_management

By default, it runs on port 15672. So don't forget to open that port. And you can access this dashboard using your.server.ip.address:15672.

Now in this Management Dashboard, go to the Admin tab and on the right side, you'll see a link to Virtual Hosts.

There will be one virtual host named / and user guest (that you cannot access without localhost). Now create a new virtual host and check if it has the same user as you just created a few moments back (admin in this case).

Connecting to the RabbitMQ server

Use the string below to connect to the server instead of amqp://localhost

amqp://admin:[email protected]/vhost_name

You can use this example (or any other examples) for the rest of the things...

I hope this helps!

like image 54
Apal Shah Avatar answered Sep 20 '22 03:09

Apal Shah


In my case, the issue was related to heartbeat. You have to pass the heartbeat as a parameter to your connection URL:

amqps://user:password@domain/vhost_name?heartbeat=30
like image 22
Abdullah Al Mamun Avatar answered Sep 19 '22 03:09

Abdullah Al Mamun