Name
SPVM::Mojo::Transaction::WebSocket - WebSocket transaction
Description
Mojo::Transaction::WebSocket class in SPVM is a container for WebSocket transactions, based on RFC 6455 and RFC 7692.
Usage
use Mojo::Transaction::WebSocket;
# Send and receive WebSocket messages
my $ws = Mojo::Transaction::WebSocket->new;
$ws->send("Hello World!");
$ws->on(message => method : void ($ws : Mojo::Transaction::WebSocket, $msg : string) { say "Message: $msg"; });
$ws->on(finish => method : void ($ws : Mojo::Transaction::WebSocket, $code : Int, $reason : string) { say "WebSocket closed with status " . (int)$code . "."; });
Super Class
Events
binary
Emitted when a complete WebSocket binary message has been received.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket, $bytes : string);
Examples:
$ws->on(binary => method : void ($ws : Mojo::Transaction::WebSocket, $bytes : string) { say "Binary: $bytes"; });
drain
Emitted once all data has been sent.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket);
Examples:
$ws->on(drain => method : void ($ws : Mojo::Transaction::WebSocket) { $ws->send(time) });
finish
Emitted when the WebSocket connection has been closed.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket, $code : int, $reason : string);
Examples:
$ws->on(finish => method : void ($ws : Mojo::Transaction::WebSocket, $code : int, $reason : string) {});
frame
Emitted when a WebSocket frame has been received.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket, $frame : Mojo::WebSocket::Frame);
Examples:
$ws->on(frame => method : void ($ws : Mojo::Transaction::WebSocket, $frame : Mojo::WebSocket::Frame) {
});
json
Emitted when a complete WebSocket message has been received, all text and binary messages will be automatically JSON decoded. Note that this event only gets emitted when it has at least one subscriber.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket, $json : object);
Examples:
$ws->on(json => method : void ($ws : Mojo::Transaction::WebSocket, $hash : object) { say "Message: " . $hash->(Hash)->get_string("msg"); });
message
Emitted when a complete WebSocket message has been received, text messages will be automatically decoded. Note that this event only gets emitted when it has at least one subscriber.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket, $msg);
Examples:
$ws->on(message => method : void ($ws : Mojo::Transaction::WebSocket, $msg) { say "Message: $msg"; });
resume
Emitted when transaction is resumed.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket);
Examples:
$tx->on(resume => method : void ($ws : Mojo::Transaction::WebSocket) {});
text
Emitted when a complete WebSocket text message has been received.
Callback:
method : void ($ws : Mojo::Transaction::WebSocket, $bytes : string);
Examples:
$ws->on(text => method : void ($ws : Mojo::Transaction::WebSocket, $bytes : string) { say "Text: $bytes"; });
Fields
compressed
has compressed : rw byte;
Compress messages with permessage-deflate
extension.
established
has established : rw byte;
WebSocket connection established.
handshake
has handshake : rw Mojo::Transaction::HTTP;
The original handshake transaction, usually a Mojo::Transaction::HTTP object.
masked
has masked : rw byte;
Mask outgoing frames with XOR cipher and a random 32-bit key.
max_websocket_size
has max_websocket_size : rw int;
Maximum WebSocket message size in bytes, defaults to the value of the SPVM_MOJO_MAX_WEBSOCKET_SIZE
environment variable or 262144
(256KiB).
Class Methods
static method new : Mojo::Transaction::WebSocket ();
Create a new Mojo::Transaction::WebSocket object, and return it.
Instance Methods
build_message
method build_message : Mojo::WebSocket::Frame ($msg : object of string|object[]);
Build WebSocket message.
Examples:
my $frame = $ws->build_message({binary => $bytes});
my $frame = $ws->build_message({text => $bytes});
my $frame = $ws->build_message({json => $data});
my $frame = $ws->build_message($chars);
client_read
method client_read : void ($chunk : string);
Read data client-side, used to implement user agents such as Mojo::UserAgent.
client_write
method client_write : string ();
Write data client-side, used to implement user agents such as Mojo::UserAgent.
closed
method closed : void ();
Same as Mojo::Transaction#completed, but also indicates that all transaction data has been sent.
connection
method connection : string ();
Connection identifier.
finish
method finish : void ($code : int = 0, $reason : string = undef);
Close WebSocket connection gracefully.
Examples:
$ws = $ws->finish;
$ws = $ws->finish(1000);
$ws = $ws->finish(1003 => "Cannot accept data!");
is_websocket
method is_websocket : int ();
True, this is a Mojo::Transaction::WebSocket object.
kept_alive
method kept_alive : int ();
Connection has been kept alive.
local_address
method local_address : string ();
Local interface address.
local_port
method local_port : int ();
Local interface port.
parse_message
method parse_message : void ($frame : Mojo::WebSocket::Frame);
Parse WebSocket message.
Examples:
$ws->parse_message($frame);
protocol
method protocol : string ();
Return negotiated subprotocol or undef
.
remote_address
method remote_address : string ();
Remote interface address.
remote_port
method remote_port : int ();
Remote interface port.
req
method req : Mojo::Message::Request ();
Handshake request, usually a Mojo::Message::Request object.
res
method res : Mojo::Message::Response ();
Handshake response, usually a Mojo::Message::Response object.
resume
method resume : void ();
Resume "handshake" transaction.
send
method send : void ($msg : object of string|object[]|Mojo::WebSocket::Frame, $cb : Mojo::EventEmitter::Callback = undef);
Send message or frame non-blocking via WebSocket, the optional drain callback will be executed once all data has been written.
Examples:
$ws = $ws->send({binary => $bytes});
$ws = $ws->send({text => $bytes});
$ws = $ws->send({json => $data);
$ws = $ws->send($frame);
$ws = $ws->send($chars);
$ws = $ws->send($chars => method : void ($ws : Mojo::Transaction::WebSocket) {});
# Send "Ping" frame
$ws->send([1, 0, 0, 0, Mojo::WebSocket->WS_PING, 'Hello World!']);
server_read
method server_read : void ($chunk : string);
Read data server-side, used to implement web servers such as Mojo::Server::Daemon.
server_write
method server_write : string ();
Write data server-side, used to implement web servers such as Mojo::Server::Daemon.
with_compression
method with_compression : void ();
Negotiate permessage-deflate
extension for this WebSocket connection.
with_protocols
method with_protocols : string ($protos : string[]);
Negotiate subprotocol for this WebSocket connection.
See Also
Copyright & License
Copyright (c) 2025 Yuki Kimoto
MIT License