@io = io end def write(object, options = {}) options.each do |k,v| @io.write "#{k}: #{v}\n" end @io.write "data: #{JSON.dump(object)}\n\n" end def close @io.close end end
include ActionController::Live def index response.headers['Content-Type'] = 'text/event-stream' sse = SSE.new(response.stream) begin loop do sse.write({ :time => Time.now }) sleep 5 end rescue IOError # When the client disconnects, we'll get an IOError on write ensure sse.close end end end