0

I am using amqplib to create amqp clients. it works fine when running on localhost, but when I change it to the server's IP address 192.168.1.44, I get an error that indicates the conn object is undefined.

this is the client's code

var amqp = require('amqplib/callback_api');
amqp.connect('amqp://guest:guest@192.168.1.44:5672', function(err, conn) { 
    conn.createChannel(function(err, ch) {
    var q = 'hello';
    ch.assertQueue(q, {durable: false});
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
    ch.consume(q, function(msg) {
    console.log(" [x] Received %s", msg.content.toString());
    }, {noAck: true});
  });
});

and this is the error message

/home/pi/Desktop/mqtt_example/receive.js:14
conn.createChannel(function(err, ch) {
    ^

TypeError: Cannot read property 'createChannel' of undefined
   at /home/pi/Desktop/mqtt_example/receive.js:14:5
   at /home/pi/Desktop/mqtt_example/node_modules/amqplib/callback_api.js:16:10
   at /home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connect.js:164:12
   at bail (/home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:176:5)
   at afterStartOk (/home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:219:7)
   at /home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:160:12
   at Socket.recv (/home/pi/Desktop/mqtt_example/node_modules/amqplib/lib/connection.js:498:12)
   at Object.onceWrapper (events.js:314:30)
   at emitNone (events.js:105:13)
   at Socket.emit (events.js:207:7)
   at emitReadable_ (_stream_readable.js:502:10)
   at emitReadable (_stream_readable.js:496:7)
   at addChunk (_stream_readable.js:263:7)
   at readableAddChunk (_stream_readable.js:239:11)
   at Socket.Readable.push (_stream_readable.js:197:10)
   at TCP.onread (net.js:588:20)
Arf Wael
  • 21
  • 1
  • 5

1 Answers1

1

I fixed it by creating a new user on the rabbitmq broker. the guest user works only with localhost. for more details see https://stackoverflow.com/a/26820152/7236105.

Arf Wael
  • 21
  • 1
  • 5
  • To be clear, the problem is that your `connect` method was not returning a connection because it was unable to connect. Your code was incorrect in trying to use the `conn` object before checking the `err` object, and had you properly checked the `err` object, there would not have been need for this question :) – theMayer Nov 27 '17 at 22:40
  • @theMayer No, in fact, this same example you can find in the rabbitmq docs but it didn't specify what to do when we change the IP in the connect method. I was using the by default guest user in the rabbitmq broker but I realized that this particular user can only access the broker via localhost. for the record I did check the `err` stack trace and it does no good. the real problem is what I stated above. you only need to create a new user with these commands `rabbitmqctl add_user test test rabbitmqctl set_user_tags test administrator rabbitmqctl set_permissions -p / test ".*" ".*" ".*"` – Arf Wael Nov 29 '17 at 10:32
  • This limitation of guest is clearly articulated in the documentation. I'm not sure exactly where off the top of my head, but it is in there. But as I said, it appears that is one of many authentication problems that you *could* have. If the "err" object does not produce a meaningful answer (I've never used this particular client) - perhaps you could raise a bug report there? – theMayer Nov 29 '17 at 13:31