Skip to content

Instantly share code, notes, and snippets.

@Shpigford
Created February 1, 2014 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shpigford/8754842 to your computer and use it in GitHub Desktop.
Save Shpigford/8754842 to your computer and use it in GitHub Desktop.
class LiveController < ApplicationController
include ActionController::Live
def stream
# Set the response header to keep client open
response.headers['Content-Type'] = 'text/event-stream'
begin
100.times {
response.stream.write("The temperature is #{ 100*rand} degrees\n")
sleep 0.5
}
response.stream.write("That's all for now!")
rescue IOError
# client disconnected
ensure
response.stream.close
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment