Dear all,

basically what I want to achieve is this:

   1. App gets an "EventSource" javascript request with some query param 
   from client
   2. App sends a HTTP request to a backend server with that query
   3. Backend server sends blocks of data in a streaming, long-running 
   response (HTTP 1.0; currently not "chunked transfer")
   4. App retrieves data fragments until it has aggredated a complete block
   5. App parses data block, sends transformed data as "Server sent Event" 
   to client
   6. GOTO 4 unless backend server finishes its response

My idea was to create a transaction

  my $tx = $ua->build_tx( GET => '...');

then have a "progress" event handler to aggregate the response chunks

  my $chunk;
  $tx->res->on("progress" => sub {
      # add new data to chunk
      # test if chunk contains complete block
      # if no: return from sub
      # if yes: process block, then $c->write( "event: block\ndata: 
$data\n\n" );
  }

and a finish handler to clean up

  $tx->res->on("finish" => sub {
      # check $chunk for leftovers
      $c->write( "event: resultend\ndata: -\n\n");
      $c->finish;
  });

And finally to start the backend transaction:

   $ua->start($tx);

This worked in so far as that the events shown above were processed, but 
the client only ever received a reponse after the backend transaction was 
closed.
I then realised that that´s because the backend transaction is called in a 
blocking way.
My - maybe naive - idea then was to remove the "finish" handler for the 
backend transaction and move it´s functionality to the callback for "start":

  $tx->res->on("progress" => sub {
      my $res = shift;
      warn "progress event: " . $res->body_size; # never seen on STDERR
  });
  $ua->start( $tx => sub {
      my ($ua, $tx) = @_;
      warn $tx->is_finished; # always 1
      $c->write( "event: resultend\ndata: -\n\n");
      $c->finish;
  });

But now the "progress" handler no longer gets called while on the other 
hand the callback to "start" is called apparently immediately and the 
transaction is marked as "finished".

Could somebody please tell me
a) if what I want to achieve is possible at all and
b) provide some hints of what I´m missing and how it could be done?

If it´s not possible with a http server backend, could it be a solution to 
hand of the http backend communication to a different process (Minion 
perhaps) which would update a Redis queue and then have my app read from 
the redis queue and send the data from there to the client?
Or are there even better solutions?

Thanks!
Heiko

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to