NAME

AnyEvent::Mojo::Server - Run Mojo apps using AnyEvent framework

VERSION

version 0.8003

SYNOPSIS

    use strict;
    use warnings;
    use AnyEvent;
    use AnyEvent::Mojo::Server;
    
    my $server = AnyEvent::Mojo::Server->new;
    $server->port(3456)->listen_queue_size(10);
    $server->max_keep_alive_requests(100)->keep_alive_timeout(3);
    
    $server->handler_cb(sub {
      my ($self, $tx) = @_;
      
      # Do whatever you want here
      $you_mojo_app->handler($tx);

      # Cool stats
      $tx->res->headers(
        'X-AnyEvent-Mojo-Request-Count' =>  $server->request_count
      );
      
      return $tx;
    });
    
    # Start it up and keep it running
    $server->run
    
    # integrate with other AnyEvent stuff
    $server->listen
    
    # other AnyEvent stuff here
    
    # Run the loop
    AnyEvent->condvar->recv;
    
    # Advanced usage: use your own Connection class
    $server->connection_class('MyConnectionClass');

STATUS

This is a first beta release. The interface should not change in a backwards incompatible way until version 1.0.

Basic HTTP/1.0 and HTTP/1.1 works, including keep-alives.

Support for 100-Continue is still missing.

DESCRIPTION

This module allows you to integrate Mojo applications with the AnyEvent framework. For example, you can run a web interface for a long-lived AnyEvent daemon.

The AnyEvent::Mojo::Server extends the Mojo::Server class.

To use you need to create a AnyEvent::Mojo::Server object. You can set the port with the port() method.

Then set the request callback with the Mojo::Server method, handler_cb().

This callback will be called on every request. The first parameter is the AnyEvent::Mojo::Server server object itself, and the second parameter is a Mojo::Transaction.

The code should build the response and return.

Optionally, you can pause the current request and resume it later. This is useful if you need to request information from a remote service to answer the current request. You pause the current Mojo request, ask the information asyncronously, and resume when the answer arrives. In the meantime, you can keep answering other requests.

METHODS

new

Creates a new AnyEvent::Server::Mojo instance.

Accepts an hash/hashref with options. All the following methods can be used as options. Check the method documentation below for valid values and an explanation about each one.

host
port
listen_queue_size
max_keep_alive_requests
keep_alive_timeout
connection_class

Returns a AnyEvent::Server::Mojo object.

host

The hostname or IP address to which the server will bind to. By default it will bind to all interfaces.

port

Port where the server will listen on. Defaults to 3000.

listen_queue_size

Defines the size of the listening queue. Defaults to SOMAXCONN .

Use

    perl -MSocket -e 'print Socket::SOMAXCONN,"\n"'

to discover the default for your operating system.

max_keep_alive_requests

Number of requests that each connection will allow in keep-alive mode.

Use 0 for unlimited requests. Default is 100 requests.

keep_alive_timeout

Number of seconds (can be fractional) that the server lets open connections stay idle.

Default is 5 seconds.

request_count

Returns the number of requests the server has answered since it started.

connection_class

Sets the class name that will be used to process each connection.

Defaults to " AnyEvent::Mojo::Server::Connection ".

listen

Starts the listening socket.

Returns nothing.

run

Starts the listening socket and kickstarts the " AnyEvent " runloop.

stop

Closes the listening socket and stops the runloop initiated by a call to run() .

startup_banner

Called after the listening socket is started. You can override this method on your " AnyEvent::Mojo::Server " subclasses to setup other components.

The default startup_banner is empty.

AUTHOR

Pedro Melo, <melo at cpan.org>

COPYRIGHT & LICENSE

Copyright 2008-2009 Pedro Melo.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

3 POD Errors

The following errors were encountered while parsing the POD:

Around line 316:

L<> starts or ends with whitespace

Around line 328:

L<> starts or ends with whitespace

Around line 340:

L<> starts or ends with whitespace