Centrifugo::Client
use Centrifugo::Client; use AnyEvent; my $cclient = Centrifugo::Client->new("$CENTRIFUGO_WS/connection/websocket"); $cclient -> on('connect', sub{ my ($infoRef)=@_; print "Connected to Centrifugo version ".$infoRef->{version}; }) -> on('message', sub{ my ($infoRef)=@_; print "MESSAGE: ".encode_json $infoRef->{data}; }) -> connect( user => $USER_ID, timestamp => $TIMESTAMP, token => $TOKEN ); $cclient->subscribe( channel => 'my-channel&' ); $cclient->subscribe( channel => 'public-channel' ); $cclient->subscribe( channel => '$private' ); # Now start the event loop to keep the program alive AnyEvent->condvar->recv;
This library allows to communicate with Centrifugo through a websocket.
my $client = Centrifugo::Client->new( $URL );
or
my $client = Centrifugo::Client->new( $URL, debug => 'true', # If true, some informations are written on STDERR debug_ws => 'true', # If true, all web socket messages are written on STDERR authEndpoint => "...", # The full URL used to ask for a key to subscribe to private channels max_alive_period => 30, # interval (in s) since last communication with server that triggers a PING (default 0) refresh_period => 5, # Check frequency for max_alive_period (default 10s) retry => 0.5 , # interval (in ms) between reconnect attempts which value grows exponentially (default 1.0) max_retry => 30, # upper interval value limit when reconnecting. (default 30) resubscribe => 'true', # automatic resubscribing on subscriptions (default: 'true') recover => 'true', # Recovers the lost messages after a reconnection (default: 'false') ws_params => { # These parameters are passed to AnyEvent::WebSocket::Client->new(...) ssl_no_verify => 'true', timeout => 600 }, );
$client->connect( user => $USER_ID, timestamp => $TIMESTAMP, token => $TOKEN, [info => $info,] [uid => $uid,] );
This function retuns $self to allow chains of multiple function calls.
It is possible to provide a UID for this command, but if you don't, a random one will be generated for you and cannot be retrieved afterward.
$client->publish( channel=>$channel, data=>$data, [uid => $uid] );
$data must be a HASHREF to a structure (which will be encoded to JSON), for example :
$client->public ( channel => "public", data => { nick => "Anonymous", text => "My message", } );
or even :
$client->public ( channel => "public", data => { } ); # Sends an empty message to the "public" channel
This function returns the UID used to send the command to the server. (a random string if none is provided)
$client->disconnect();
$client->subscribe( channel => $channel, [ uid => $uid ,] );
If the channel is private (starts with a '$'), then a request to $this->{AUTH_URL} is done automatically to get the channel key.
If channel contains a '&', then the function adds the client_id behind the client channel boundary. (even after a reconnect)
This function returns the UIDs used to send the command to the server. (a random string if none is provided)
$client->unsubscribe( channel => $channel, [ uid => $uid ] );
$client->presence( channel => $channel, [ uid => $uid ] );
$client->history( channel => $channel, [ uid => $uid ] );
$client->ping( [ uid => $uid ] );
Known events are 'message', 'connect', 'disconnect', 'subscribe', 'unsubscribe', 'publish', 'presence', 'history', 'join', 'leave', 'refresh', 'ping', 'ws_closed', 'ws_error'
$client->on( 'connect', sub { my( $dataRef ) = @_; ... });
(this function retuns $self to allow chains of multiple function calls)
Note : Events that are an answer to the client requests (ie 'connect', 'publish', ...) have an 'uid' which is added into the %data structure.
$client->client_id()
$key = Centrifugo::Client::generate_token($secret, $user, $timestamp [,$info])
INPUT : $secret is the private secret key, only known by the server.
$user is the user name. $timestamp is the current timestamp. $info is a JSON encoded string.
The same function may be used to generate a private channel key :
$key = generate_token($secret, $client, $channel [,$info])
INPUT : $client is the client_id given when connected to Centrifugo.
$channel is the name of the channel (should start with a '$' as it is private).
And to sign each request to access to the HTTP API :
$sign = generate_token($self, $data)
INPUT : $data is a JSON string with your API commands
To install Centrifugo::Client, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Centrifugo::Client
CPAN shell
perl -MCPAN -e shell install Centrifugo::Client
For more information on module installation, please visit the detailed CPAN module installation guide.