#!/usr/bin/env perl 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 'https://code.jquery.com/jquery-2.1.4.min.js'; %= javascript 'https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.js'
##
:~$ morbo t1.pl
Server available at http://127.0.0.1:3000
[Wed Jun 17 21:54:11 2015] [debug] Your secret passphrase needs to be changed
[Wed Jun 17 21:54:11 2015] [debug] GET "/"
[Wed Jun 17 21:54:11 2015] [debug] Rendering template "index.html.ep" from DATA section
[Wed Jun 17 21:54:11 2015] [debug] Template "layouts/basic.html.ep" not found
[Wed Jun 17 21:54:11 2015] [debug] 200 OK (0.005384s, 185.736/s)
[Wed Jun 17 21:54:11 2015] [debug] GET "/data"
[Wed Jun 17 21:54:11 2015] [debug] Routing to a callback
[Wed Jun 17 21:54:11 2015] [debug] 101 Switching Protocols (0.000710s, 1408.451/s)