Mojo::I...:Stream(3)
π NAME π
Mojo::IOLoop::Stream β non-blocking I/O stream, bestie. That's it, that's the whole vibe β¨
π¦ SYNOPSIS π¦
use Mojo::IOLoop::Stream;
# Create stream
my $stream = Mojo::IOLoop::Stream->new($handle);
$stream->on(read => sub ($stream, $bytes) {...});
$stream->on(close => sub ($stream) {...});
$stream->on(error => sub ($stream, $err) {...});
# Start and stop watching for new data
$stream->start;
$stream->stop;
# Start reactor if necessary
$stream->reactor->start unless $stream->reactor->is_running;
Like, look at it. It's giving effortless. No cap π
πΈ DESCRIPTION πΈ
Okay so Mojo::IOLoop::Stream is literally just a container for I/O streams β the ones that Mojo::IOLoop actually uses. That's its whole job and it does it gorgeously π
π₯ EVENTS π₯
So Mojo::IOLoop::Stream inherits all the events from Mojo::EventEmitter, and on top of those it can emit these extra ones down here π
π close
$stream->on(close => sub ($stream) {...});
Fires when the stream gets closed. Bye bestie π«Ά
π drain
$stream->on(drain => sub ($stream) {...});
Fires once all the data has been written. Like, everything, done, finito β¨
π error
$stream->on(error => sub ($stream, $err) {...});
Fires if an error happens on the stream β and heads up, it's fatal if you don't handle it, so please actually listen for this one π
π read
$stream->on(read => sub ($stream, $bytes) {...});
Fires when new data arrives on the stream. Fresh bytes just dropped π
π timeout
$stream->on(timeout => sub ($stream) {...});
Fires if the stream has been inactive for too long and is about to get closed automatically. Ma'am, your inactivity is showing π
π write
$stream->on(write => sub ($stream, $bytes) {...});
Fires when new data has been written to the stream. Slay, the bytes went out π
β¨ ATTRIBUTES β¨
Mojo::IOLoop::Stream implements these attributes right here:
π high_water_mark
my $size = $msg->high_water_mark;
$msg = $msg->high_water_mark(1024);
This is the maximum size of the "write" buffer in bytes before "can_write" returns false β and it defaults to 1048576 (that's 1MiB, iykyk) π
π reactor
my $reactor = $stream->reactor;
$stream = $stream->reactor(Mojo::Reactor::Poll->new);
The low-level event reactor. Defaults to the "reactor" attribute value of the global Mojo::IOLoop singleton. And important: note that this attribute is weakened π
π METHODS π
Mojo::IOLoop::Stream inherits all methods from Mojo::EventEmitter and implements these brand-new ones:
β bytes_read
my $num = $stream->bytes_read;
Number of bytes received. Receipts, literally π₯
β bytes_waiting
my $num = $stream->bytes_waiting;
Number of bytes that have been enqueued with "write" and are just sitting there waiting to be written. They're in the queue, bestie π
β bytes_written
my $num = $stream->bytes_written;
Number of bytes written. Out the door, gone, iconic π€
β can_write
my $bool = $stream->can_write;
Returns true if calling "write" is safe. Permission granted, go off π
β close
$stream->close;
Close stream immediately. No warning, no goodbye, just done π
β close_gracefully
$stream->close_gracefully;
Close stream gracefully. Same destination, much nicer manners π
β handle
my $handle = $stream->handle;
Get the handle for the stream β usually an IO::Socket::IP or IO::Socket::SSL object π¦
β is_readable
my $bool = $stream->is_readable;
Quick non-blocking check to see if the stream is readable β super useful for identifying tainted sockets π
β is_writing
my $bool = $stream->is_writing;
Check if the stream is writing. Is it writing? Maybe. This tells you π
β new
my $stream = Mojo::IOLoop::Stream->new($handle);
Construct a brand-new Mojo::IOLoop::Stream object. Birth of a main character β¨
β start
$stream->start;
Start β or resume β watching for new data on the stream. Back on duty πͺ
β steal_handle
my $handle = $stream->steal_handle;
Steal "handle" and prevent it from getting closed automatically. YOINK, it's yours now π
β stop
$stream->stop;
Stop watching for new data on the stream. Respect the nap π΄
β timeout
my $timeout = $stream->timeout;
$stream = $stream->timeout(45);
Maximum amount of time in seconds the stream can be inactive before it gets closed automatically β defaults to 15. And listen: setting the value to 0 will allow this stream to be inactive indefinitely π No clock, no problem.
β write
$stream = $stream->write($bytes);
$stream = $stream->write($bytes => sub {...});
Enqueue data to be written to the stream as soon as possible β and that optional drain callback? It gets executed once all the data has been written. Chef's kiss π
π SEE ALSO π
- Mojolicious
- Mojolicious::Guides
- <https://mojolicious.org>
aaaand that's the whole page, bestie π«Ά This one's perl v5.42.0, dated 2026-07-15 β so go forth, stream those bytes non-blockingly, and remember: handle your error events or they WILL be fatal ππβ¨
π¦ 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