how do I run a ruby script with an event machine listener on heroku?

543 views Asked by At

I have a script which uses a gem that has eventmachine which listens for an API call (it's the slack-api gem).

On my dev environment, I just run bundle exec ruby ruby_slack.rb and the console shows that it is listening. When the API call hits, then I see the stdout.

How do I have this same behavior in heroku?

I created a Procfile which has: web: bundle exec ruby slack.rb but I think it's just waiting for a request from the web app to run it.

I want this to run (and keep running) listening for events.

Thanks.

1

There are 1 answers

0
dB. On

You need a web server that responds on the port that Heroku asks it to respond and a thread for websocket processing. For example with Sinatra:

require 'sinatra/base'

module MyBot
  class Web < Sinatra::Base
    get '/' do
      'Hello world.'
    end
  end
end

Thread.new do
  # run your Slack RealTime bot here
end

run MyBot::Web

This is captured in this tutorial. Also generally try slack-ruby-bot which does all the heavy lifting for you.