0

I'm trying to implement server send events in Rails 4 running on passenger. The aim is send the client updates when a active record changes.

There is a pretty neat example in php over here https://stackoverflow.com/a/31255914

But I was wondering how this is achieved in rails. Is there any specific configuration required for rails to manage the connections ?

Community
  • 1
  • 1
Dave
  • 656
  • 2
  • 6
  • 22

1 Answers1

0

Found the solution here: http://api.rubyonrails.org/classes/ActionController/Live.html

Just include the Action Controller live, and write a simple action:

class MyController < ActionController::Base
  include ActionController::Live

  def stream
    response.headers['Content-Type'] = 'text/event-stream'
    100.times {
      response.stream.write "hello world\n"
      sleep 1
    }
  ensure
    response.stream.close
  end
end
Dave
  • 656
  • 2
  • 6
  • 22