The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::IOLoop - IO Loop

SYNOPSIS

    use Mojo::IOLoop;

    # Create loop and listen on port 3000
    my $loop = Mojo::IOLoop->new;
    $loop->listen(port => 3000);

    # Accept connections
    $loop->accept_cb(sub {
        my ($self, $id) = @_;

        # Incoming data
        $self->read_cb($id => sub {
            my ($self, $id, $chunk) = @_;

            # Got some data, time to write
            $self->writing($id);
        });

        # Ready to write
        $self->write_cb($id => sub {
            my ($self, $id) = @_;

            # Back to reading only
            $self->not_writing($id);

            # The loop will take care of buffering for us
            return 'HTTP/1.1 200 OK';
        });
    });

    # Start and stop loop
    $loop->start;
    $loop->stop;

DESCRIPTION

Mojo::IOLoop is a general purpose IO loop for TCP clients and servers, easy to subclass and extend.

ATTRIBUTES

Mojo::IOLoop implements the following attributes.

accept_cb

    my $cb = $loop->accept_cb;
    $loop  = $loop->accept_cb(sub { ... });

accept_timeout

    my $timeout = $loop->accept_timeout;
    $loop       = $loop->accept_timeout(5);

clients

    my $clients = $loop->clients;
    $loop       = $loop->clients(25);

connect_cb

    my $cb = $loop->connect_cb;
    $loop  = $loop->connect_cb(sub { ... });

connect_timeout

    my $timeout = $loop->connect_timeout;
    $loop       = $loop->connect_timeout(5);

lock_cb

    my $cb = $loop->lock_cb;
    $loop  = $loop->lock_cb(sub { ... });

max_clients

    my $max = $loop->max_clients;
    $loop   = $loop->max_clients(1000);

servers

    my $servers = $loop->servers;
    $loop       = $loop->servers(25);

unlock_cb

    my $cb = $loop->unlock_cb;
    $loop  = $loop->unlock_cb(sub { ... });

timeout

    my $timeout = $loop->timeout;
    $loop       = $loop->timeout(5);

METHODS

Mojo::IOLoop inherits all methods from Mojo::Base and implements the following new ones.

new

    my $loop = Mojo::IOLoop->new;

connect

    my $c = $loop->connect(address => '127.0.0.1', port => 3000);
    my $c = $loop->connect({address => '127.0.0.1', port => 3000});

connection_timeout

    my $timeout = $loop->connection_timeout($id);
    $loop       = $loop->connection_timeout($id => 45);

drop

    $loop = $loop->drop($id);

error_cb

    $loop = $loop->error_cb($id => sub { ... });

finish

    $loop = $loop->finish($id);

hup_cb

    $loop = $loop->hup_cb($id => sub { ... });

listen

    $loop->listen(port => 3000);
    $loop->listen({port => 3000});

local_info

    my $info = $loop->local_info($id);

not_writing

    $loop->not_writing($id);

read_cb

    $loop = $loop->read_cb($id => sub { ... });

remote_info

    my $info = $loop->remote_info($id);

start

    $loop->start;

stop

    $loop->stop;

write_cb

    $loop = $loop->write_cb($id => sub { ... });

writing

    $loop->writing($id);