Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: real-time output from Mojolicious WebSockets?

by Anonymous Monk
on Dec 19, 2014 at 13:34 UTC ( #1110853=note: print w/replies, xml ) Need Help??

in reply to real-time output from Mojolicious WebSockets?

Well I don't know any Mojolicious but
$self->on(&myapp($tx) => sub { ...
Wouldn't that be evaluated as
$self->on('You should never see this.' => sub { ...
after &myapp($tx) returns and before registering the handler?

Also, please tell why you're using ampersands before function names.

$ws->send("Time is: " . scalar(localtime())); ## odd, we never see this output
Maybe because Mojolicious takes 'You should never see this.' seriously? :)

Or maybe I'm wrong and Mojo is just too magical for people like me...

Replies are listed 'Best First'.
Re^2: real-time output from Mojolicious WebSockets?
by Anonymous Monk on Dec 19, 2014 at 21:43 UTC
Re^2: real-time output from Mojolicious WebSockets?
by BroFish (Initiate) on Dec 19, 2014 at 22:41 UTC

    Why ampersands you ask? Because I started programming in perl late last century :), and that's how you called procedural functions back then. Today I call OO methods via $obj-<method(), but I still call procedural functions the old fashioned way.

    "As iron sharpens iron, so one man sharpens his friend." So I'm open being brought into the 21st century. :) Perhaps I should read "Modern Perl"?

    To your point, and the point of the other anonmonk, I am slightly perplexed on how to call my own web service. I did try:
    $self->on(json => sub...
    but the affects were still the same in that output from the anonymous procedure called, or methods called from inside that anonymous procedure, etc. was being saved until the procedure ended, not being delivered as it was produced. (I stripped down my code to produce this simplified example. I would be happy to repost using "on(json" if you'd like.)

      ... but the affects were still the same in ...

      :) in that one subroutine the comments says odd, we never see this output but Mojo doesn't have a "You should never see this." event, so it makes sense that sub is never executed and output is never seen ... mojo will never call it

      OTOH why the buffering happens, is odd ... I'll take a closer look later

      OTOH, standard way of doing this, instead of calling a sub that sleeps, Proc::Background a process that sleeps, basic technique from Watching long processes through CGI (Aug 02)

      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 Mojo::Template and perltidy -olq  -csc -csci=3 -cscl="sub : BEGIN END if while " -otr -opr -ce -nibc -i=4 -pt=0   "-nsak=*"

        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

        There might be better ways ... its best to consult mojo irc channel for that ... maybe check the logs at

        #!/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() ); #~ #~ 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() ) + " : " +; $('#result').append( "\n" + ( (new Date()).toLocal +eString() ) + " : " +; }; % end </body> </html>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1110853]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-06-13 17:03 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (57 votes). Check out past polls.