1

I am trying to get an EventMachine server that runs when I run my rails server and not as a separate process.

So for example, using the simple server example from here (em-websocket) you get the following (standalone?) ruby code:

EventMachine.run {

EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
    ws.onopen {
      puts "WebSocket connection open"

      # publish message to the client
      ws.send "Hello Client"
    }

    ws.onclose { puts "Connection closed" }
    ws.onmessage { |msg|
      puts "Recieved message: #{msg}"
      ws.send "Pong: #{msg}"
    }
end
}

Assuming I just ran the command "rails new em-example" where does the above code go in my rails 3 app?

I would imagine the above code should ideally run in a separate thread (or fiber..?), but am unsure how that looks.

I've seen that this should be easy if using 'thin' as my webserver. Heroku seems to use this by default and I've installed the 'thin' gem so I can test it out locally as well.

Adam
  • 131
  • 9
  • @SrdjanPejic What's a better "set up" for hosting EM on Heroku? Or is that a bad idea? – Adam Dec 17 '11 at 03:37
  • This may be useful http://stackoverflow.com/questions/5799406/how-do-you-spawn-an-eventmachine-inside-a-rails-app – Blue Smith Sep 18 '12 at 03:50

1 Answers1

1

You'll probably want to toss your code into an initializer. John Nunemaker posted some really interesting info about firing up EventMachine under Passenger in a Sinatra app, and I'd imagine it would work fairly similarly for you.

That said, it sounds like you're just trying to get a web sockets server working with Heroku. In theory youd think running on the Cedar stack and adding a new process type to your Procfile for your EM based Websockets server would get it working. That would look something like:

web:         bundle exec rails server -p $PORT
websocket:   bundle exec script/websocket_server $PORT

With script/websocket_server being your app's socket server script that can handle a custom port argument.

Unfortunately, though, Heroku doesn't support Websockets yet, as outlined here, so a Procfile solution most likely wouldn't work.

commondream
  • 504
  • 3
  • 5
  • Thanks for the explanation. Any idea how else I can get a websocket server hosted somewhere if Heroku doesn't support it? Whether it be EM or nodejs... – Adam Dec 17 '11 at 03:59
  • What are you trying to do with Websockets? I really like [Pusher](http://pusher.com/), which is a service that handles pushing data to the browser, but it doesn't do two-way communication. That may work for you. You could also check out Socket.IO. You can run it on Heroku using the info in [this doc](http://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku). It'll use polling though, not true websockets. Socket.IO is a node.js framework though, so you wouldn't be working with EventMachine, or even Ruby for that matter. – commondream Dec 17 '11 at 04:05
  • Doing some more reading it looks like I'll want to do this in node.js. I want to tackle a collaborative text-editing app from scratch (I know there's a bunch of examples out there already... just want to do it for fun). – Adam Dec 17 '11 at 04:15