++ed by:

1 PAUSE user(s)
1 non-PAUSE user(s).

Graham Ollis


AnyEvent::WebSocket::Connection - WebSocket connection for AnyEvent


version 0.22


 # send a message through the websocket...
 $connection->send('a message');
 # recieve message from the websocket...
 $connection->on(each_message => sub {
   # $connection is the same connection object
   # $message isa AnyEvent::WebSocket::Message
   my($connection, $message) = @_;
 # handle a closed connection...
 $connection->on(finish => sub {
   # $connection is the same connection object
   my($connection) = @_;
 # close an opened connection
 # (can do this either inside or outside of
 # a callback)

(See AnyEvent::WebSocket::Client or AnyEvent::WebSocket::Server on how to create a connection)


This class represents a WebSocket connection with a remote server or a client.

If the connection object falls out of scope then the connection will be closed gracefully.

This class was created for a client to connect to a server via AnyEvent::WebSocket::Client, and was later extended to work on the server side via AnyEvent::WebSocket::Server. Once a WebSocket connection is established, the API for both client and server is identical.



The underlying AnyEvent::Handle object used for the connection. WebSocket handshake MUST be already completed using this handle. You should not use the handle directly after creating AnyEvent::WebSocket::Connection object.

Usually only useful for creating server connections, see below.


If set to true, it masks outgoing frames. The default is false.



Send a message to the other side. $message may either be a string (in which case a text message will be sent), or an instance of AnyEvent::WebSocket::Message.

$connection->on($event => $cb)

Register a callback to a particular event.

For each event $connection is the AnyEvent::WebSocket::Connection and and $message is an AnyEvent::WebSocket::Message (if available).


 $cb->($connection, $message)

Called each time a message is received from the WebSocket.


 $cb->($connection, $message)

Called only for the next message received from the WebSocket.



Called when the connection is terminated


Close the connection.


Although written originally to work with AnyEvent::WebSocket::Client, this class was designed to be used for either client or server WebSocket connections. For details, contact the author and/or take a look at the source for AnyEvent::WebSocket::Client and the examples that come with Protocol::WebSocket.


The methods in this section are deprecated and may be removed from a future version of this class. They should not be used for new code, and are only remain documented here to aid in understanding legacy code that use them.


Register a callback to be called on each subsequent message received. The message itself will be passed in as the only parameter to the callback. The message is a decoded text string.


Register a callback to be called the next message received. The message itself will be passed in as the only parameter to the callback. The message is a decoded text string.


Register a callback to be called when the connection is closed.



author: Graham Ollis <plicease@cpan.org>


Toshio Ito


This software is copyright (c) 2013 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

Hosting generously
sponsored by Bytemark