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 to 0.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 for IO::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 🌈

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