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 🌟


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