streaming content (chunked)

85 views
Skip to first unread message

John Saya

unread,
Jun 4, 2014, 10:26:04 PM6/4/14
to mojol...@googlegroups.com
I'm trying to create a simple server using Mojo, that can stream content back to a browser.  Basically it would be just a couple lines to send.

I figured using chunked transfer encoding would do the trick, but the client doesn't display any data until the final "0" chunk is sent.  I'm guessing there's some sort of buffering happening, and even came across "drain", but can't seem to get it to work.  Below is the code in it's simplest form.

In this example, I would expect "HI" to display on the client and 5 seconds later "LO" to display:

Mojo::IOLoop->server({port => 9000} => sub {

    my ($loop, $stream) = @_;

    $loop   = $loop->max_connections(1000);
   
    $stream->on(drain => sub {
 
    print "Drain\n";
#            $stream->write("0\r\n\r\n");
    });
   
    $stream->on(read => sub {
  
        my ($stream, $bytes) = @_;

        print "Bytes: $bytes\n";
     
        $stream->write("HTTP/1.1 200 OK\r\n");
        $stream->write("Connection: keep-alive\r\n");
        $stream->write("Transfer-Encoding: chunked\r\n");
        $stream->write("Content-Type: text/plain\r\n\r\n");
      

       $stream->write("2\r\n");
       $stream->write("HI\r\n");

        sleep(5);

               
        $stream->write("2\r\n");
        $stream->write("LO\r\n");

        $stream->write("0\r\n\r\n");
       
        });


});

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

sri

unread,
Jun 4, 2014, 10:30:19 PM6/4/14
to mojol...@googlegroups.com
I'm trying to create a simple server using Mojo, that can stream content back to a browser.

You do know Mojolicious has native support for the chunked transfer encoding, right?


--
sebastian 

John Saya

unread,
Jun 4, 2014, 10:39:37 PM6/4/14
to mojol...@googlegroups.com
Thanks for the quick reply.  I did see that, but I guess I'm just not sure how to get it working within my code.  Would you be able to show how to get that working within IOLoop->server in my sample code?


--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/JhtOtraArLI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

sri

unread,
Jun 4, 2014, 10:41:50 PM6/4/14
to mojol...@googlegroups.com, js...@webpost.net
Thanks for the quick reply.  I did see that, but I guess I'm just not sure how to get it working within my code.  Would you be able to show how to get that working within IOLoop->server in my sample code?

If you want to know how to build an HTTP server you can always look at the source of Mojo::Server::Daemon.

--
sebastian 
Reply all
Reply to author
Forward
0 new messages