http://www.perlmonks.org?node_id=1110899


in reply to Re^2: real-time output from Mojolicious WebSockets?
in thread real-time output from Mojolicious WebSockets?

Yup, somebody is definitely buffering the messages (mojo)

Connection open. 12/19/2014, 5:08:52 PM : Entering log4. 12/19/2014, 5:08:52 PM : Leaving log4. 12/19/2014, 5:09:06 PM : Entering myapp. 12/19/2014, 5:09:06 PM : 0...Fri Dec 19 17:08:54 2014 12/19/2014, 5:09:06 PM : 1...Fri Dec 19 17:08:57 2014 12/19/2014, 5:09:06 PM : 2...Fri Dec 19 17:09:00 2014 12/19/2014, 5:09:06 PM : 3...Fri Dec 19 17:09:03 2014 12/19/2014, 5:09:06 PM : Leaving myapp.

Forking the job in the background and doing some IPC ought to work , but may not be satisfying :)

My version of your code with Re: indenter / prettyprinter / beautifier / perltidy for mojolicio.us Mojo::Template and perltidy -olq  -csc -csci=3 -cscl="sub : BEGIN END if while " -otr -opr -ce -nibc -i=4 -pt=0   "-nsak=*"

#!/usr/bin/perl -- # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 fea +tures use Mojolicious::Lite; Main( @ARGV ); exit( 0 ); sub myapp { my $tx = shift; $tx->send( "Entering myapp." ); foreach my $xx ( 0 .. 3 ) { $tx->send( "$xx...".localtime() ); sleep( 3 ); } $tx->send( "Leaving myapp." ); return "You should never see this."; } ## end sub myapp sub Main { get '/' => sub { my $c = shift; $c->render( 'index' ); }; get '/log2' => sub { my $c = shift; $c->render( 'log2' ); }; websocket '/log3' => \&ws_log4; app->secrets( [ 'password' => '8675309J' ] ); app->start; } ## end sub Main sub ws_log4 { my $self = shift; my $tx = $self->tx; my $ip = $tx->remote_address; app->log->debug( "Client '$ip' connected" ); $tx->send( "Entering log4." ); $self->inactivity_timeout( 50 ); Mojo::IOLoop->timer( 2 => sub { myapp( $tx ) } ); ## get started $self->on( finish => sub { my( $c, $code, $reason ) = @_; $c->app->log->debug( "WebSocket closed with status $code." + ); } ); $tx->send( "Leaving log4." ); } ## end sub ws_log4 __DATA__ @@ index.html.ep <html> <head> <title>Static Page</title> </head> <body> <h1>Index Page</h1> <p>This is a static page. For WebSockets example, click <a href="/log2">here</a> +. </p> </body> </html> @@ log2.html.ep <html> <head> <title>WebSockets Example</title> %= javascript '/mojo/jquery/jquery.js' </head> <body> <p> wait for it ) <form><textarea id="result" cols="50" rows="20"></textarea +></form> %= javascript begin var ws = new WebSocket('<%= url_for('log3')->to_abs %> +'); ws.onopen = function() { $('#result').text("Connection open."); ws.send("Hi."); }; ws.onmessage = function (e) { // $('#result').append( "\n" + ( (new Date()).toIS +OString() ) + " : " + e.data); $('#result').append( "\n" + ( (new Date()).toLocal +eString() ) + " : " + e.data); }; % end </body> </html>

Replies are listed 'Best First'.
Re^4: real-time output from Mojolicious WebSockets?
by Anonymous Monk on Dec 20, 2014 at 04:28 UTC

    ok, timer is concurrent, for delay after N seconds its a bit more typing (should be some kind of helper ) ... but this is still cooperative multitasking type deal, if any one of your steps blocks, well, then it blocks :) got to this from https://metacpan.org/pod/Mojolicious::Guides::Cookbook#REAL-TIME-WEB

    There might be better ways ... its best to consult mojo irc channel for that ... maybe check the logs at http://irclog.perlgeek.de/mojo

    #!/usr/bin/perl -- # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 fea +tures use Mojolicious::Lite; Main( @ARGV ); exit( 0 ); sub myapp { my $tx = shift; $tx->send( "Entering myapp." ); my @delays; foreach my $xx ( 0 .. 3 ) { $tx->send( "$xx...".localtime() ); #~ https://metacpan.org/pod/Mojo::IOLoop#delay #~ https://metacpan.org/pod/Mojo::IOLoop::Delay push @delays, sub { my ($delay, @args) = @_; Mojo::IOLoop->timer( 3 => $delay->begin ); $tx->send( "after 3 $xx...".localtime() ); }; } $tx->send( "Leaving myapp." ); Mojo::IOLoop->delay( @delays )->wait; return "You should never see this."; } ## end sub myapp sub Main { get '/' => sub { my $c = shift; $c->render( 'index' ); }; get '/log2' => sub { my $c = shift; $c->render( 'log2' ); }; websocket '/log3' => \&ws_log4; app->secrets( [ 'password' => '8675309J' ] ); app->start; } ## end sub Main sub ws_log4 { my $self = shift; my $tx = $self->tx; my $ip = $tx->remote_address; app->log->debug( "Client '$ip' connected" ); $tx->send( "Entering log4." ); $self->inactivity_timeout( 50 ); Mojo::IOLoop->timer( 2 => sub { myapp( $tx ) } ); ## get started $self->on( finish => sub { my( $c, $code, $reason ) = @_; $c->app->log->debug( "WebSocket closed with status $code." + ); } ); $tx->send( "Leaving log4." ); } ## end sub ws_log4 __DATA__ @@ index.html.ep <html> <head> <title>Static Page</title> </head> <body> <h1>Index Page</h1> <p>This is a static page. For WebSockets example, click <a href="/log2">here</a> +. </p> </body> </html> @@ log2.html.ep <html> <head> <title>WebSockets Example</title> %= javascript '/mojo/jquery/jquery.js' </head> <body> <p> wait for it ) <form><textarea id="result" cols="70" rows="30"></textarea +></form> %= javascript begin var ws = new WebSocket('<%= url_for('log3')->to_abs %> +'); ws.onopen = function() { $('#result').text("Connection open."); ws.send("Hi."); }; ws.onmessage = function (e) { // $('#result').append( "\n" + ( (new Date()).toIS +OString() ) + " : " + e.data); $('#result').append( "\n" + ( (new Date()).toLocal +eString() ) + " : " + e.data); }; % end </body> </html>
      oh right, the output is
      Connection open. 12/19/2014, 8:30:17 PM : Entering log4. 12/19/2014, 8:30:17 PM : Leaving log4. **pause here** 12/19/2014, 8:30:19 PM : Entering myapp. 12/19/2014, 8:30:19 PM : 0...Fri Dec 19 20:30:19 2014 12/19/2014, 8:30:19 PM : 1...Fri Dec 19 20:30:19 2014 12/19/2014, 8:30:19 PM : 2...Fri Dec 19 20:30:19 2014 12/19/2014, 8:30:19 PM : 3...Fri Dec 19 20:30:19 2014 12/19/2014, 8:30:19 PM : Leaving myapp. 12/19/2014, 8:30:19 PM : after 3 0...Fri Dec 19 20:30:19 2014 **pause here** 12/19/2014, 8:30:22 PM : after 3 1...Fri Dec 19 20:30:22 2014 **pause here** 12/19/2014, 8:30:25 PM : after 3 2...Fri Dec 19 20:30:25 2014 **pause here** 12/19/2014, 8:30:28 PM : after 3 3...Fri Dec 19 20:30:28 2014
      naturally it pauses at **pause here**

      proc background if you're doing something which you can't fanagle into cooperative multitasking

        actually i have same question,
        There is long-time operation on server and I want update its progress on client-side.
        In several points on server-side code I call $web_socket->send(...) and process it on client-side ws.onmessage = function (event) {...};
        Server-side logic as below:
        some_computation1();
        $web_socket->send('computation1 end');
        ...
        some_computation15();
        $web_socket->send('computation15 end');
        ...
        some_computation100();
        $web_socket->send('computation100 end. All ok!');
        Client-side as below
        ws = new WebSocket(url);
        ws.onmessage = function (event) {
        $('#log_view').append('

        ' + event.data + '

        ');
        };
        ws.onopen = function (event) {
        ...
        };
        ws.onclose = function (event) {
        ...
        };
        Everything work fine, but not in realtime: all messages has received by client at one big bulk list and only after whole server-side script has finished.
        in fact, i need every message show up after one step finished.
        appreciate if any friends can help me.