title: How can I push to Faye Server from Rails Controller?
body: <p>I have this code: </p>
  
  <pre><code>def create
      message = Message.new(text: params[:message][:text], author: params[:message][:author])
      if message.save
        render json: {result: 'success'}
      else
        render json: {result: 'failure'}
      end
    end
  </code></pre>
  
  <p>I have client subscribed to Faye Server:</p>
  
  <pre><code>var subscription = client.subscribe('/foo', function (message) {
      getMessages();
  });
  </code></pre>
  
  <p>I want to publish some message to Faye when a message is created. As listed in Faye Ruby Sever documentation, I have to do like this:</p>
  
  <pre><code>require 'eventmachine'
  
  EM.run {
    client = Faye::Client.new('http://localhost:9292/faye')
  
    client.subscribe('/foo') do |message|
      puts message.inspect
    end
  
    client.publish('/foo', 'text' =&gt; 'Hello world')
  }
  </code></pre>
  
  <p>But when I paste this code into my <code>create</code> method, it blocks the rails thread with EventMachine and server doesn't work any more.</p>
  
  <p>When I use <code>client.publish</code> without <code>EventMachine</code>, I get an error.</p>
  
  <p>How can I publish to Faye from server? I know that there are gems like <code>faye-rails</code> or <code>private_pub</code>, but I want to figure out how to do it myself. Is there any way to integrate EventMachine and Rails? Maybe I should run EventMachine on a different thread?</p>