http://qs1969.pair.com?node_id=1130920

rkrasowski has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody, I started looking into websocket as a way to push data from server to browser. I found example for Mojolicious written by Joel Berger. I am trying to make it to work, but no luck so far. here is the code:

websocket.pl

use Mojolicious::Lite; any '/' => 'index'; websocket '/data' => sub { my $self = shift; my $timer = Mojo::IOLoop->recurring( 1 => sub { state $i = 0; $self->send({ json => gen_data($i++) }); }); $self->on( finish => sub { Mojo::IOLoop->remove($timer); }); }; sub gen_data { my $x = shift; return [ $x, sin( $x + 2*rand() - 2*rand() ) ] } app->start; __DATA__ @@ index.html.ep % layout 'basic'; %= javascript '/jquery-1.9.1.min.js' %= javascript '/jquery.flot.js' <div id="plot" style="width:600px;height:300px"> </div> %= javascript begin var data = []; var plot = $.plot($('#plot'), [ data ]); var url = '<%= url_for('data')->to_abs %>'; var ws = new WebSocket( url ); ws.onmessage = function(e){ var point = JSON.parse(e.data); data.push(point); plot.setData([data]); plot.setupGrid(); plot.draw(); }; % end

While starting the script with morbo websocket.pl commands everything seems to be fine, but when I connect from the browser to the server I am getting following messages:

debug Your secret passphrase needs to be changed

debug GET "/"

debug Rendering template "index.html.ep" from DATA section

debug Template "layouts/basic.html.ep" not found

debug 200 OK (0.043215s, 23.140/s)

debug GET "/jquery-1.9.1.min.js"

debug Template "not_found.development.html.ep" not found

debug Template "not_found.html.ep" not found

debug Rendering inline template "fd403ab55a4c875e35b42428816134c7"

debug Rendering inline template "dd85456cf9f44e1e43bebae130a7f9df"

debug 404 Not Found (0.201262s, 4.969/s)

debug GET "/jquery.flot.js"

debug Template "not_found.development.html.ep" not found

debug Template "not_found.html.ep" not found

debug Rendering cached inline template "fd403ab55a4c875e35b42428816134c7"

debug Rendering cached inline template "dd85456cf9f44e1e43bebae130a7f9df"

debug 404 Not Found (0.051227s, 19.521/s)

Any idea what's wrong?

Thanks like always

Robert