Mojo::I...:Server(3)
π
Mojo::IOLoop::Server (also living on your system as Mojo::I...:Server(3), the manual page for Mojo::IOLoop::Server in the User Contributed Perl Documentation, bestie, don't panic about the truncated name)
okay so. picture this: you need a server. not a whole framework situation, just something that quietly listens for connections and hands them to you. that's this. π
β¨ NAME β¨
Mojo::IOLoop::Server β a non-blocking TCP and UNIX domain socket server. no cap, that's the whole job and it does it gorgeously. π
πΈ SYNOPSIS πΈ
use Mojo::IOLoop::Server;
# Create listen socket
my $server = Mojo::IOLoop::Server->new;
$server->on(accept => sub ($server, $handle) {...});
$server->listen(port => 3000);
# Start and stop accepting connections
$server->start;
$server->stop;
# Start reactor if necessary
$server->reactor->start unless $server->reactor->is_running;
see how clean that is? that's the vibe. β¨
π¦ DESCRIPTION π¦
Mojo::IOLoop::Server accepts TCP/IP and UNIX domain socket connections, and it does it for Mojo::IOLoop. it's the friend who stands at the door and lets people in. literally that. π«Ά
π₯ EVENTS π₯
Mojo::IOLoop::Server inherits all events from Mojo::EventEmitter, and on top of that it can emit the following new one. yes, one. quality over quantity bestie. π
π accept
$server->on(accept => sub ($server, $handle) {...});
emitted for each accepted connection. every single one. it's giving doorman energy. πͺβ¨
π ATTRIBUTES π
Mojo::IOLoop::Server implements the following attributes.
β reactor
my $reactor = $server->reactor;
$server = $server->reactor(Mojo::Reactor::Poll->new);
the low-level event reactor. it defaults to the "reactor" attribute value of the global Mojo::IOLoop singleton. and heads up β this attribute is weakened, so don't go thinking you can just cling to it forever without holding a real reference somewhere. π
π METHODS π
Mojo::IOLoop::Server inherits all methods from Mojo::EventEmitter and implements the following new ones. buckle up, we're going through every single one. π
π generate_port
my $port = Mojo::IOLoop::Server->generate_port;
finds a free TCP port for you. primarily used for tests, but honestly? it's iconic in its own little way. π
π§ handle
my $handle = $server->handle;
gets the handle for the server β usually an IO::Socket::IP object. usually. iykyk. π«Ά
π is_accepting
my $bool = $server->is_accepting;
checks if connections are currently being accepted. a little vibe check for your server, fr fr. π
πΈ listen
$server->listen(port => 3000);
$server->listen({port => 3000});
creates a new listen socket. note that TLS support depends on IO::Socket::SSL (2.009+) β so if TLS is your whole personality, make sure that's installed, bestie.
these options are currently available:
addressβ the local address to listen on, defaults to0.0.0.0.
address => '127.0.0.1'
backlogβ maximum backlog size, defaults to"SOMAXCONN". yes, with the quotes and everything. that's the literal default.
backlog => 128
fdβ file descriptor with an already prepared listen socket. coming in prepped and ready, love that for you.
fd => 3
pathβ path for the UNIX domain socket to listen on.
path => '/tmp/myapp.sock'
portβ port to listen on, defaults to a random port. surprise! π
port => 80
reuseβ allows multiple servers to use the same port with the"SO_REUSEPORT"socket option. sharing is caring.
reuse => 1
single_acceptβ only accept one connection at a time. one at a time. patience is a virtue.
single_accept => 1
tlsβ enable TLS. π
tls => 1
tls_caβ path to the TLS certificate authority file.
tls_ca => '/etc/tls/ca.crt'
tls_certβ path to the TLS cert file, defaults to a built-in test certificate.
tls_cert => '/etc/tls/server.crt'
tls_cert => {'mojolicious.org' => '/etc/tls/mojo.crt'}
tls_keyβ path to the TLS key file, defaults to a built-in test key.
tls_key => '/etc/tls/server.key'
tls_key => {'mojolicious.org' => '/etc/tls/mojo.key'}
tls_optionsβ additional options forIO::Socket::SSL.
tls_options => {SSL_alpn_protocols => ['foo', 'bar'], SSL_verify_mode => 0x00}
π port
my $port = $server->port;
gets the port this server is listening on. yes, this server. the one you're holding. π
π start
$server->start;
starts or resumes accepting connections. resume is the key word there, don't forget it. β¨
π¦ stop
$server->stop;
stops accepting connections. that's it. that's the whole method. periodt. π
π SEE ALSO π
MojoliciousMojolicious::Guides- <https://mojolicious.org>
go read them, they're literally so good. π
and that's the page, bestie π«Ά β brought to you by perl v5.42.0, dated 2026-07-15, same source and version and date as the original, just with way more sparkle. mwah. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms