Skip to content

Instantly share code, notes, and snippets.

@justinhillsjohnson
Created July 22, 2013 01:59
Show Gist options
  • Save justinhillsjohnson/6050862 to your computer and use it in GitHub Desktop.
Save justinhillsjohnson/6050862 to your computer and use it in GitHub Desktop.
Rails SSE controller
require 'sse'
class BrowserController < ApplicationController
include ActionController::Live
def index
# SSE expects the `text/event-stream` content type
response.headers['Content-Type'] = 'text/event-stream'
sse = Reloader::SSE.new(response.stream)
begin
loop do
@count = Pledge.count
sse.write({ :time => Time.now, :count => @count}, :event => 'refresh')
sleep 1
end
rescue IOError
# When the client disconnects, we'll get an IOError on write
ensure
sse.close
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment