Top Banner
Mojolicious Perl Framework for the Real-Time Web Dotan Dimet @dotandimet
32

Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Jul 12, 2015

Download

Software

Dotan Dimet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Mojolicious

Perl Framework for the Real-Time Web

Dotan Dimet@dotandimet

Page 2: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Page 3: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

But that was a while back

Most People Moved on

Page 4: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Perl People

• But some came back

• And brought back cool ideas

Page 5: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Page 6: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Mojolicious

Page 7: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Mojolicious

Page 8: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Page 9: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Page 10: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Real Time Web

• Non-blocking I/O

• Event-driven code

• Asynchronous APIs

– Not mandatory

– Sometimes necessary

Page 11: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Features

Page 12: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Page 13: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Morbo error page

Page 14: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Hypnotoad error page

Page 15: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Exampleuse Mojolicious::Lite;

use 5.20.0;

use experimental 'signatures';

get '/' => {template => 'index'};

websocket '/title' => sub ($c) {

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

};

app->start;

__DATA__

@@ index.html.ep

% my $url = url_for 'title';

<script>

var ws = new WebSocket('<%= $url->to_abs %>');

ws.onmessage = function (event) { document.body.innerHTML += event.data };

ws.onopen = function (event) { ws.send('http://mojolicio.us') };

</script>

Page 16: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Page 17: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

First Route:render a template

get '/' => {template => 'index'};

Page 18: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

use Mojolicious::Lite;

use 5.20.0;

use experimental 'signatures';

get '/' => {template => 'index'};

websocket '/title' => sub ($c) {

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

};

app->start;

__DATA__

@@ index.html.ep

% my $url = url_for 'title';

<script>

var ws = new WebSocket('<%= $url->to_abs %>');

ws.onmessage = function (event) { document.body.innerHTML += event.data };

ws.onopen = function (event) { ws.send('http://mojolicio.us') };

</script>

Page 19: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Template (embedded Perl)

% my $url = url_for 'title';

<script>

var ws = new WebSocket('<%= $url->to_abs %>');

ws.onmessage = function (event) { document.body.innerHTML += event.data };

ws.onopen = function (event) { ws.send('http://mojolicio.us') };

</script>

Page 20: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Websocket Message Event

websocket '/title' => sub ($c) {

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

};

Event driven – subscribe to message event, triggered when complete message arrives

Page 21: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Websocket Message Event

websocket '/title' => sub ($c) {

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

};

• Send response when done

Page 22: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Built-in Web Client

websocket '/title' => sub ($c) {

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

};

Get a web page, parse into DOM, return the title text.

Page 23: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Web Client

• Mojo::UserAgent

– Blocking and non-blocking HTTP requests

• Mojo::DOM

– Supports most CSS selectors like jQuery

Page 24: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Web Client 2

• Get Haaretz.co.il full article text:self->ua->get( $url,

{ 'User-Agent' => 'Googlebot/2.1; +http://www.google.com/bot.html)' }

)->res->dom->find('#article-box p')->join("\n");

Page 25: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

But this is blocking code!

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

When we trigger the message event, we block the event loop until we get a response from the web page

Page 26: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Sometimes blocking is OK

• Traditional Database connection

• Small apps, querying fast remote sites

Page 27: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

But we don’t have to block

$c->ua->get($url, sub { ... });

The Mojo::UserAgent calls can take a callback to make non-blocking requests

Page 28: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Sometimes we can’t block

$c->ua->get($c->url_for(‘index’)->to_abs)

• For example, when demoing without internet, we want to fetch a web page we are serving from the same process...

Page 29: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Page 30: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

More

• http://mojolicio.us

• http://mojocasts.com

– Glen Hinkle @tempire

• (I stole the nice slides from him)

• irc irc://irc.perl.org/#mojo

• mailing list [email protected]

Page 31: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

use Mojolicious::Lite;

use 5.20.0;

use experimental 'signatures';

get '/' => {template => 'index'};

websocket '/title' => sub ($c) {

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

};

app->start;

__DATA__

@@ index.html.ep

% my $url = url_for 'title';

<script>

var ws = new WebSocket('<%= $url->to_abs %>');

ws.onmessage = function (event) { document.body.innerHTML += event.data };

ws.onopen = function (event) { ws.send('http://mojolicio.us') };

</script>

Page 32: Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)

Signaturesuse Mojolicious::Lite;

Use 5.20.0;

Use experimental 'signatures';

get '/' => {template => 'index'};

websocket '/title' => sub ($c) {

$c->on(message => sub ($c, $msg) {

my $title = $c->ua->get($msg)->res->dom->at('title')->text;

$c->send($title);

});

};

app->start;

__DATA__

@@ index.html.ep

% my $url = url_for 'title';

<script>

var ws = new WebSocket('<%= $url->to_abs %>');

ws.onmessage = function (event) { document.body.innerHTML += event.data };

ws.onopen = function (event) { ws.send('http://mojolicio.us') };

</script>