Integrating EventMachine with Rails can be a complicated experience. We worked through some of the compatibility issues and share our solution here.
Behind the scenes at HiringThing, we use Ruby on Rails for our application server, and RabbitMQ to pass events to our Scala-based analytics engine. For a long time, we were
using the Carrot gem for Rails to Rabbit communication. But Carrot is a sychronous client, which means waiting for message confirmation
before moving on. The alternative is the AMQP gem , which is asychronous, but requires
EventMachine to work correctly.
Between that and implementing Pusher , we decided having async capability built-in to keep HiringThing response times snappy was worth the effort. That said,
integrating EventMachine with Rails is no bed of roses. EventMachine needs to run in it’s own thread… sometimes. If you’re using Thin for a web server (which we use in development),
it has EventMachine built in. But if you you’re using Passenger (which we use in production), its way of forking processes can cause orphaned threads. Finally, if you’re using Capybara for
testing (which we are), then there’s third way to make it work.
We worked through this with plenty of trial and error, thanks to bits of code here and there posted online, and now have a working and stable Rails/EventMachine initializer that works well in all three cases. Thought I’d share it – I really
wish I’d run across this blog post early in the process of getting started.