views:

55

answers:

1

Is it possible for a perl cgi script to segment its AJAX responses into numerous individual HTTP responses?

Say I have this code:

xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        onDataReceived(xmlhttp.responseText);
    }
    else if(xmlhttp.status!=200 && xmlhttp.status!=0) {    }
}
xmlhttp.open("POST","script.cgi",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(toURLString(options));

as javascript (dont tell me about xml object compatibility issues with ie, I know, and don't care).

and this:

print "Content-type: text/html\n\n";

my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
    $|=1;
    for(my $i, (1..100000000))
    {
        print "1\n";
    }
}

as perl cgi. Is it possible to print out this result in numerous individual packets of 1s, instead of generating 100000000 1s before finally having an output?

+1  A: 

Please see this SO question for possible approaches, though it's not Perl specific:

http://stackoverflow.com/questions/1155066/dealing-with-incremental-server-response-in-ajax-in-javascript

From the linked Wiki article, this link seems most relevant: http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest

However, I would strongly suggest considering a polling approach instead of the "server push" one you are considering:

The server stores the chunks of data as accessible files (with some ordering meta info)

print "Location: xxxx"; 
# Sorry, forgot the exact form of Location HTTP response.
# Location points to URL mapped to /home/htdocs/webdocs/tmp/chunk_0.html
my %form = Vars();
if($ENV{REQUEST_METHOD} eq "POST" )
{
    $|=1;
    $file_num = 0;
    my $fh;
    for(my $i, (1..100000000))
    {
        if ($i % 1000 == 0) {
            close $fh if $fh;
            open $fh, ">", "/home/htdocs/webdocs/tmp/chunk_${file_num}.html";
            # Add the usual error handling on open/close i'm too lazy to type
            $file_num++;
        }
        print $fh "1\n";
    }
    print $fh "\n##############END_TRANSMISSION__LAST_FILE####################\n";
    # This was a singularly dumb way of marking EOF but you get the drift
    close $fh;
}

The AJAX poller retrieves them in a loop one by one, processing the response containing the next chunk and looking for meta-info to know what (and if) the next piece to poll for is.

DVK
Obviously naming schema needs improvement, e.g. include session ID or timestamp of request, for concurrency reasons. Plus implemt a separate CGI to clean up, which could be called via AJAX once the last response is read in.
DVK
Just to be clear - the above code was meant as a rough illustration of the idea, not a good well-designed or well-implemented actual solution.
DVK
Awesome solution, I tried this out and it works, now I'll have to decide whether the design of solution fits in my program. Thanks :)Also "# Add the usual error handling on open/close i'm too lazy to type" hahaha, that probably took just as much writing as"or die "unable to open";" hahah
Razor Storm
Oh yeah, I guess it is probably wise to send the client back a response to let the javascript know that an output is started in the file.
Razor Storm
@Razor Storm - FYI - the `die` message should always include `$!` for IO errors; and I generally hate dying in a web app... which is why I did a generic "insert error handling here" hand-waving instead :)
DVK