# bundle exec thin start -R config.ru -p 9292
# config.ru
# run with `thin start -p 9292`
require 'rack/stream'
class App
include Rack::Stream::DSL
stream do
after_open do
count = 0
@timer = EM.add_periodic_timer(1) do
if count != 3
chunk "chunky #{count}\n"
count += 1
else
# Connection isn't closed until #close is called.
# Useful if you're building a firehose API
close
end
end
end
before_close do
@timer.cancel
chunk "monkey!\n"
end
[200, {'Content-Type' => 'text/plain'}, []]
end
end
app = Rack::Builder.app do
use Rack::Stream
run App.new
end
run app
Result
# Server
$ bundle exec thin start -R config.ru -p 9292
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on 0.0.0.0:9292, CTRL+C to stop
# Client
curl http://localhost:9292
chunky 0
chunky 1
chunky 2
monkey!