NAME

Fetch::WebSocket - a live WebSocket connection from Fetch

SYNOPSIS

my $ws = $ua->websocket('ws://host/echo')->get;   # after the 101

$ws->send('hello');                 # text (UTF-8)
$ws->send_binary($bytes);           # binary
my $reply = $ws->next_message->get; # Future for the next message

$ws->on_message(sub { my ($msg) = @_; ... });
$ws->on_close(sub { warn "closed\n" });
$ws->close;

DESCRIPTION

A WebSocket (RFC 6455) connection, returned by $ua->websocket($url). The HTTP/1.1 Upgrade handshake and all framing are native C (masking, message reassembly, automatic ping/pong and close); this object drives the same non-blocking connection and event loop as an ordinary request, so it composes with Futures like everything else in Fetch.

METHODS

send($text) / send_binary($bytes)

Send a text (UTF-8) or binary message.

next_message

A Fetch::Future resolving to the next inbound message (text is decoded, binary is raw bytes). Fails if the socket has closed.

on_message($cb) / on_close($cb)

Install a persistent per-message callback (buffered messages are delivered at once), or a one-shot close callback.

is_closed / close

Whether the socket has closed; send a close frame and shut it down.

AUTHOR

LNATION <email@lnation.org>

LICENSE AND COPYRIGHT

This software is Copyright (c) 2026 by LNATION. This is free software, licensed under the Artistic License 2.0.