Skip to content

Instantly share code, notes, and snippets.

@geku
Created May 29, 2013 16:21
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 geku/5671589 to your computer and use it in GitHub Desktop.
Save geku/5671589 to your computer and use it in GitHub Desktop.
Streaming example with CURB
require 'sinatra/base'
server = Thread.new do
class Example < Sinatra::Base
set :server, :thin
get '/' do
stream(:keep_open) do |out|
5.times {
out << "streaming output\n"
sleep 1
}
out.close
end
end
run!
end
end
sleep 2
# CURB client
require 'curb'
curl = Curl::Easy.new
curl.url = 'http://localhost:4567/'
curl.on_body do |data|
puts ">> #{data}"; data.size
end
curl.http(:GET)
server.exit
@hanshasselberg
Copy link

That works with Typhoeus out of the box:

Typhoeus.get("localhost:4567/").body
#=> "streaming output\nstreaming output\nstreaming output\nstreaming output\nstreaming output\n"

Edit: oh. You don't see the single lines... do you need that? Ethon might me be suited then...

@geku
Copy link
Author

geku commented May 30, 2013

Yes I'm interested in every single line as soon as it arrives. My use case is basically a long running server process that streams e.g. log output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment