views:

1234

answers:

1

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";

?>
+1  A: 

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.

Dave
good point about Mongrel, I'm using passenger because of that.
Zach
I'm assuming from this thread (http://markmail.org/message/3g7yvrq2oum6a5h4#query:+page:1+mid:yhiw6xqawegwokfg+state:results) this does not work except when using FastCGI.
Kris