Multipart response in Ruby/Rack
Question
I want my server to send a multipart response (multipart/x-mixed-replace). I'd prefer some kind of solution using the Sinatra framework or a generic Rack app, but any example in ruby would be nice. Here's the equivalent of what I'm trying to do, in PHP:
<?php
header('Content-type: multipart/x-mixed-replace;boundary="rn9012"');
print "--rn9012\n";
print "Content-type: application/xml\n\n";
print "<?xml version='1.0'?>\n";
print "<content>First Part</content>\n";
print "--rn9012\n";
flush();
sleep(5);
print "Content-type: application/xml\n\n";
print "<?xml version='1.0'?>\n";
print "<content>Second Part</content>\n";
print "--rn9012--\n";
?>
Solution
You can probably use the out.flush method for this:
class TestController < ApplicationController
def index
render :text => lambda { |resp, out|
out.puts 'start'
out.flush
10.times do
out.puts '.'
out.flush
sleep 1
end
out.puts 'done'
}
end
end
However, keep in mind that if you're using Mongrel to serve your Ruby code (as many people using RoR do), you won't be able to stream at all.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow