Mojo::WebSocket(3)
π Mojo::WebSocket β the WebSocket protocol, bestie
okay so this is Mojo::WebSocket, from the User Contributed Perl Documentation crew β and it's literally the whole WebSocket protocol in one little module. we're gonna get through it together, i promise π«Ά
β¨ SYNOPSIS β¨
the little taste test, look:
use Mojo::WebSocket qw(WS_TEXT build_frame parse_frame);
my $bytes = build_frame 0, 1, 0, 0, 0, WS_TEXT, 'Hello World!';
my $frame = parse_frame \$bytes, 262144;
two lines. that's it. that's the whole vibe check. π
πΈ DESCRIPTION πΈ
so Mojo::WebSocket implements the WebSocket protocol exactly as described in RFC 6455 (https://tools.ietf.org/html/rfc6455). icon behavior, no cap.
β οΈ heads up bestie: 64-bit frames need a Perl that actually supports quads β otherwise you're stuck at 32-bit. it's giving "limitations", and not the cute kind π
π FUNCTIONS π
Mojo::WebSocket implements these functions, and you can import each one individually. pick your faves, they don't come as a package deal π
π¦ build_frame
my $bytes = build_frame $masked, $fin, $rsv1, $rsv2, $rsv3, $op, $payload;
builds a WebSocket frame. that's the whole thing. here's every flavor, in order, no skipping, we eat the whole plate π
# Masked binary frame with FIN bit and payload
say build_frame 1, 1, 0, 0, 0, WS_BINARY, 'Hello World!';
# Text frame with payload but without FIN bit
say build_frame 0, 0, 0, 0, 0, WS_TEXT, 'Hello ';
# Continuation frame with FIN bit and payload
say build_frame 0, 1, 0, 0, 0, WS_CONTINUATION, 'World!';
# Close frame with FIN bit and without payload
say build_frame 0, 1, 0, 0, 0, WS_CLOSE, '';
# Ping frame with FIN bit and payload
say build_frame 0, 1, 0, 0, 0, WS_PING, 'Test 123';
# Pong frame with FIN bit and payload
say build_frame 0, 1, 0, 0, 0, WS_PONG, 'Test 123';
masked binary? β text without FIN? β continuation glued onto the end? β close with nothing inside? β ping? β pong? β literally all of them slay π₯
π§ challenge
my $bool = challenge Mojo::Transaction::WebSocket->new;
checks the WebSocket handshake challenge. returns a bool, so you get a nice little true/false situation π
β client_handshake
my $tx = client_handshake Mojo::Transaction::HTTP->new;
does the WebSocket handshake client-side. you're the one reaching out, main character energy π
π parse_frame
my $frame = parse_frame \$bytes, $limit;
parses a WebSocket frame. and check this β it gives you back the good stuff by index:
# Parse single frame and remove it from buffer
my $frame = parse_frame \$buffer, 262144;
say "FIN: $frame->[0]";
say "RSV1: $frame->[1]";
say "RSV2: $frame->[2]";
say "RSV3: $frame->[3]";
say "Opcode: $frame->[4]";
say "Payload: $frame->[5]";
it parses a single frame and removes it from the buffer, so your buffer stays clean. we love a tidy icon β¨
π server_handshake
my $tx = server_handshake Mojo::Transaction::HTTP->new;
does the WebSocket handshake server-side. other side of the same coin, still iconic πͺ
π CONSTANTS π
Mojo::WebSocket also gives you these constants, each importable individually. little opcodes for your little frames π
WS_BINARYβ opcode for "Binary" frames.WS_CLOSEβ opcode for "Close" frames.WS_CONTINUATIONβ opcode for "Continuation" frames.WS_PINGβ opcode for "Ping" frames.WS_PONGβ opcode for "Pong" frames.WS_TEXTβ opcode for "Text" frames.
all six, accounted for, nobody left behind π«Ά
π DEBUGGING π
you want the advanced diagnostics tea? set the MOJO_WEBSOCKET_DEBUG environment variable and it prints all of it to STDERR. iykyk π
MOJO_WEBSOCKET_DEBUG=1
that's it. that's the whole switch. turn it on and watch the gossip flow π
π SEE ALSO π
MojoliciousMojolicious::Guideshttps://mojolicious.org
and that's the page bestie!! π ngl for a manpage it kinda ate. this was perl v5.42.0, dated 2026-07-15, and it's Mojo::WebSocket(3) β go build some frames, periodt π¦πΈπ
π¦ 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