HTTP Streaming

6 views
Skip to first unread message

Pat

unread,
Oct 30, 2009, 3:52:07 PM10/30/09
to sinatrarb
Hi, I'm new to Sinatra, but I'm loving it so far. One issue I'm
having though is when I try to do an HTTP stream, my client doesn't
receive the data as it is sent, only at the very end of the response.
I'm sure it's something I'm doing wrong with my server. I'm basically
trying to do something like this, but through the Sinatra framework:

http://bence-bmc.blogspot.com/2008/09/http-streaming-ruby-client.html

Any help would be greatly appreciated.

Thanks in advance,
Pat

Ryan Tomayko

unread,
Oct 30, 2009, 7:59:45 PM10/30/09
to sina...@googlegroups.com

A route may return any object that responds to #each. For servers that
support iterative response generation, the response is sent as it's
iterated over:

class HelloStreamer
def each
yield "Hello"
sleep 5
yield "World!"
end
end

require 'sinatra'
get '/' do
content_type 'text/plain'
Streamer.new
end

Hitting that with curl, you should see "Hello" and then "World" after a delay.

This works with Mongrel, Unicorn, and Passenger -- maybe with WEBrick.
Thin and anything run under shotgun do not send responses iteratively.

Thanks,
Ryan

Pat

unread,
Oct 30, 2009, 9:37:19 PM10/30/09
to sinatrarb
Thanks Ryan, I got it to work!

Pat

unread,
Nov 2, 2009, 3:44:09 PM11/2/09
to sinatrarb
Whelp everything was working fine until I put nginx on top for load
balancing. It no longer streams the data, it'll send it all at the
very end of the request. I'm sure this is more of a nginx config
issue, but I'm new to nginx and figured someone here might know the
answer off the top of their head.

Thanks,
Pat

Pat

unread,
Nov 2, 2009, 4:13:21 PM11/2/09
to sinatrarb
Never mind, I found the answer...

proxy_buffering off;

Pat

unread,
Nov 3, 2009, 2:36:30 PM11/3/09
to sinatrarb
I need to keep the stream open and continuously send data until the
client closes the connection. I've been searching for a way to
determine when the client has disconnected (need to do some stuff
after this occurs), but I haven't been able to find anything. Is this
possible via Sinatra?

Thanks,
Pat
Reply all
Reply to author
Forward
0 new messages