RabbitMQ: Connecting & publishing to an existing queue in Ruby

2.5k views Asked by At

I have two process types on Heroku: a web dyno in Ruby and a worker in Node.js. I'm using the RabbitMQ addon (currently beta) to pass a message from Ruby to Node. Node connects and consumes correctly, and Ruby connects and publishes correctly as long as it is the first to connect / create the queue.

Apparently, Carrot throws some funny errors when you try to create a queue that already exists, which is how I discovered that the reason for not being able to get my message across (I could have sworn it worked when I tested last night) was that I started my Node process before my Ruby.

Since I'm on Heroku, I'm going to have more than one of each Ruby and Node threads working concurrently, and they each need to support being the first to start a queue and connect into an existing queue, without issue.

Which brings me to my question:

How do I connect to an existing RabbitMQ queue, using Ruby, for the purpose of publishing messages to consumers which are already connected and waiting to receive messages?

2

There are 2 answers

1
Jordan Warbelow-Feldstein On BEST ANSWER

Carrot will silently fail if there is a collission with an existing queue.

In order to connect to an existing queue, without colliding, you must specify the same options used when you first created the queue.

It sucks that Carrot silently fails in this case, but that's what it is.

Ruby:

Carrot.server
q = Carrot.queue('onboarding', {:durable=>true, :autoDelete=>false})
q.publish('test')

Node.js:

var amqp = require("amqp");
var c = amqp.createConnection({ host: 'localhost' });

q = c.queue('onboarding', {durable: true, autoDelete:false}); 

// ... wait for queue to connect (1 sec), or use .addListener('ready', callback) ... 

q.subscribe( {ack:true}, function(message){
  console.log(message.data.toString())
  q.shift()
})
2
alexis On

Have you tried the other client(s)?

http://rubyamqp.info/