First off, you’ll need to install RabbitMQ. Instructions for OSX here. Once you’ve done that, install the pure ruby amqp library:
gem sources -a http://gems.github.com
gem install tmm1-amqp
You’re good to go. Now open up two IRB sessions. Paste the following code into the first session:
require 'mq'
EM.run {
amq = MQ.new
EM.add_periodic_timer(1) { amq.queue("noises").publish("moo") }
}
Your publishing code has to run inside of an Event Machine loop. You can start it using EM.run. If you’re running inside of an Evented Container such as Thin or Evented Mongrel, you can skip this. The meat of it is just amq.queue("noises").publish("moo")that.
Now open a second irb session in another terminal tab, and paste this in:
require 'mq'
EM.run {
amq = MQ.new
amq.queue("noises").subscribe { |noise|
puts noise
}
}
You’ll get the moos off the Queue “noises”. No polling required, which is very nice, you simply register a callback with amqp, and it’ll be called with the message when one becomes available.
Niceness!



