5

I am using the node-amqp library to connect to a on-line stomp service, before I was using stomp-client, it was connected very successfully, but it doesn't support auto failure detection and reconnect, so I want to switch to node-amqp for more robust support.

var amqp = require('amqp');
var option = { 
  host: 'host'
, port: 61618
, login: 'my username'
, password: 'my password'
};

var implOpts = {
      reconnect: true,
      reconnectBackoffStrategy: 'exponential',
      reconnectBackoffTime: 500
};

var connection  = amqp.createConnection(option,implOpts);
connection.addListener('ready', function(){
    console.log('ready connection ');

});

connection.on('error', function (error) {
    console.log('Connection error' ,error);

});

connection.on('close', function () {
    console.log('Connection close ');

});

the hostname, password, username and port are correct and working in stomp-client library example. However by working with the code above, I got a error saying connection error { message: 'Connection ended: possibly due to an authentication failure.' }. I looked through the code, didn't find any problem about my authentication or and code.

Here is the working code in stomp-client library.

var StompClient = require('stomp-client').StompClient;

var client = new StompClient('host', 61618, 'my username', 'my password', '1.0');

client.connect(function(sessionId) {
   console.log('Trying to connect the real time service...');

}); 

could anybody tell me how to work with node-amqp to connect stomp service

user824624
  • 7,077
  • 27
  • 106
  • 183

1 Answers1

0

Not sure if you found the answer to this but I found myself beating my head on this problem and hit this near the top of the google search, so I figured I'd share my discovery:

Some Quick Info;

  • RabbitMQ 3.5.6
  • NodeJS 6.2.2
  • amqplib 0.4.2

After digging around I found the project on github at and dug into the tests to find https://github.com/squaremo/amqp.node/blob/master/test/connect.js which has a plain authentication example. The key I found was that you have to call a special function to format the credentials and then pass them as an object:

var credentials = require('amqplib/lib/credentials');
var options = {};
options.credentials = credentials.plain(configuration.rabbitmq.user, configuration.rabbitmq.pass);
amqp.connect(connstr, options ....

There was also mention of embedding the username and password into the URL, ie:

amqp://user:pass@server:port

However this didn't work for my case.

aetherwalker
  • 111
  • 1
  • 5