The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::WebSocket::Streamer - Stream a WebSocket message easily

SYNOPSIS

Here’s the gist of it:

    #Use the ::Client or ::Server subclass as needed.
    my $streamer = Net::WebSocket::Streamer::Client->new('binary');

    my $frame = $streamer->create_chunk($buf);

    my $last_frame = $streamer->create_final($buf);

… but a more complete example might be this: streaming a file of arbitrary size in 64-KiB chunks:

    my $size = -s $rfh;

    while ( read $rfh, my $buf, 65536 ) {
        my $frame;

        if (tell($rfh) == $size) {
            $frame = $streamer->create_final($buf);
        }
        else {
            $frame = $streamer->create_chunk($buf);
        }

        syswrite $wfh, $frame->to_bytes();
    }

You can, of course, create/send an empty final frame for cases where you’re not sure how much data will actually be sent.

Note that the receiving application won’t necessarily have access to the individual message fragments (i.e., frames) that you send. Web browsers, for example, only expose messages, not frames. You may thus be better off sending full messages rather than frames.

EXTENSION SUPPORT

To stream custom frame types (or overridden classes), you can subclass this module and define frame_class_* constants, where * is the frame type, e.g., text, binary.