Skip to content

Commit

Permalink
added another real-time web recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 11, 2012
1 parent 6095f6f commit ad6b929
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -229,7 +229,7 @@ into alien environments like foreign event loops.

The real-time web is a collection of technologies that include Comet
(long-polling), EventSource and WebSockets, which allow content to be pushed
to consumers as soon as it is generated with long-lived connections, instead
to consumers with long-lived connections as soon as it is generated, instead
of relying on the more traditional pull model. All built-in web servers use
non-blocking I/O and are based on the L<Mojo::IOLoop> reactor, which provides
many very powerful features that allow real-time web applications to scale up
Expand Down Expand Up @@ -274,7 +274,7 @@ won't block any other requests that might be processed in parallel.

use Mojolicious::Lite;

# Wait for 3 seconds before rendering a response
# Wait 3 seconds before rendering a response
get '/' => sub {
my $self = shift;
Mojo::IOLoop->timer(3 => sub {
Expand Down Expand Up @@ -445,6 +445,29 @@ HTTP protocol for transport.
The C<message> event will be emitted for every new log message and the
C<finish> event right after the transaction has been finished.

=head2 Event loops

Internally the L<Mojo::IOLoop> reactor can use multiple event loop backends,
L<EV> for example will be automatically used if installed. Which in turn
allows L<AnyEvent> to just work.

#!/usr/bin/env perl
use Mojolicious::Lite;
use EV;
use AnyEvent;

# Wait 3 seconds before rendering a response
get '/' => sub {
my $self = shift;
my $w;
$w = AE::timer 3, 0, sub {
$self->render(text => 'Delayed by 3 seconds!');
undef $w;
};
};

app->start;

=head1 USER AGENT

When we say L<Mojolicious> is a web framework we actually mean it.
Expand Down

0 comments on commit ad6b929

Please sign in to comment.