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 🌈


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