# HTTP progressive output / server push

**URL:** <https://www.ruby-forum.com/t/http-progressive-output-server-push/111465>\
**Category:** Rails\
**Created:** [August 7, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/http-progressive-output-server-push/111465 "2007-08-07T16:47:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [August 7, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/http-progressive-output-server-push/111465/1 "2007-08-07T16:47:13Z")

</div>

Is it possible to override the standard Rails rendering infrastructure  
for a single controller action, in such a way that I can generated  
“progressive” HTTP output? That is, the browser can display text as it  
is generated, rather than Rails buffering the whole page and sending  
it once at completion?

Here’s a trivial example, written as a CGI:

* * *

## #!/usr/bin/ruby -w $stdout.sync = true puts “Content-Type: text/html” puts puts “”, " " \* 256 puts “” (1…10).each do |i| puts “

#{i}

”  
sleep 1  
end  
puts “”

If you stick this in /cgi-bin/ and point a browser at it then you can  
see the numbers going down the screen one per second. (The real-world  
application I have is for displaying progress during a long-running  
operation, such as uploading firmware to a router). I can’t write the  
above in Rails currently.

And here’s another example, which I found on the web, using x-mixed-  
replace:

* * *

## #!/bin/sh #echo “HTTP/1.0 200” echo “Content-type: multipart/x-mixed-replace;boundary=— ThisRandomString—” echo “” echo “—ThisRandomString—” while true do echo “Content-type: text/html” echo “” echo “

## Processes on this machine updated every 5 seconds
”  
echo "time: "  
date  
echo “

”  
echo “”  
ps -el  
echo “—ThisRandomString—”  
sleep 5  
done

Now, clearly I can run these as CGIs, but then I would lose Rails’  
lovely routing and parameter handling, and its integration with  
ActiveRecord with persistent database connections.

Basically I want something like this:

def myaction  
…  
browser.puts “string1”  
…  
browser.puts “string2”  
…  
render :none=\>true  
end

For me the key issue is: do the underlying web servers that Rails runs  
under, such as webrick and mongrel, allow the response to be sent in  
parts? Is there some IO-like object hanging around that I could call  
‘puts’ or ‘write’ on to send data directly back to the browser?

There are other ways to achieve what I want, but they add a lot of  
complexity and are less real-time. For example, the browser can be set  
to poll for status every 5 seconds, but then one Rails process is  
doing work and has to publish its progress to some object, which  
another Rails process checks for data.

Other ideas are welcomed.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [August 7, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/http-progressive-output-server-push/111465/2 "2007-08-07T18:46:29Z")

</div>

Should be possible with a custom mongrel handler.

On 8/7/07, candlerb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## #!/usr/bin/ruby -w
> 
> * * *
> 
> echo “
> 
> ## Processes on this machine updated every 5 seconds
> ”  
> Now, clearly I can run these as CGIs, but then I would lose Rails’  
> …  
> to poll for status every 5 seconds, but then one Rails process is
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [August 8, 2007, 10:36am UTC](https://www.ruby-forum.com/t/http-progressive-output-server-push/111465/3 "2007-08-08T10:36:09Z")

</div>

> Should be possible with a custom mongrel handler.

Thank you, that gave me some useful keywords to put into Google 🙂

But if I understand correctly, I would still lose all of Rails routing

- I’d just get a raw HTTP request object from Mongrel?

Regards, Brian.
