Using Pusher with the Unicorn web server in Rails

We recently changed our web server from Thin to Unicorn. This caused our pusher integration to break. After some digging, we found out the cause was something like this:

def send_cool_event
  Pusher["fancy-pants-channel"].trigger_async('cool-event', pants_json)
end

The Pusher trigger_async uses EventMachine for it's asynchronicity (not sure if that's really a word). Unicorn does not have EventMachine so these triggers were no longer actually triggering. Triggering is obviously an important part of the function that a trigger does, so this caused problems.

The way to fix this is to not use Pusher's trigger_async method. Instead handle your own asynchronicity (the more I use that word, the more I think it either really is one, or at least should be). To do this (using delayed job as that's our background worker), we did something like this:

def send_cool_event
  Pusher["fancy-pants-channel"].trigger('cool-event', pants_json)
end
handle_asynchronously :send_cool_event

We are now using trigger instead of trigger_async and we use delayed_job to run that method. Of course you may need to alter your code a bit more to make sure it works well in a delayed_job or whatever else you use.

By Joel Friedlaender