title: EventMachine in Rails - where to put run loop?
body: <p><strong>I am trying to get an EventMachine server that runs when I run my rails server and not as a separate process.</strong></p>
  
  <p>So for example, using the simple server example from <a href="https://github.com/igrigorik/em-websocket" rel="nofollow">here</a> (em-websocket) you get the following (standalone?) ruby code:</p>
  
  <pre><code>EventMachine.run {
  
  EventMachine::WebSocket.start(:host =&gt; "0.0.0.0", :port =&gt; 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
  }
  </code></pre>
  
  <p><strong>Assuming I <em>just</em> ran the command "rails new em-example" where does the above code go in my rails 3 app?</strong></p>
  
  <p>I would imagine the above code should ideally run in a separate thread (or fiber..?), but am unsure how that looks.</p>
  
  <p>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.</p>