NAME

Centrifugo::Client

SYNOPSIS

 use Centrifugo::Client;
 use AnyEvent;

 my $cclient = Centrifugo::Client->new("$CENTRIFUGO_WS/connection/websocket");

 $cclient->connect(
		user => $USER_ID,
		timestamp => $TIMESTAMP,
		token => $TOKEN
	) -> on('connect', sub{
		my ($infoRef)=@_;
		print "Connected to Centrifugo version ".$infoRef->{version};
		
		# When connected, client_id() is defined, so we can subscribe to our private channel
		$cclient->subscribe( '&'.$cclient->client_id() );
		
	})-> on('message', sub{
		my ($infoRef)=@_;
		print "MESSAGE: ".encode_json $infoRef->{data};

	});

 # Now start the event loop to keep the program alive
 AnyEvent->condvar->recv;
	

DESCRIPTION

This library allows to communicate with Centrifugo through a websocket.

FUNCTION connect

$client->connect( user => $USER_ID, timestamp => $TIMESTAMP, token => $TOKEN );

FUNCTION disconnect

$client->disconnect();

FUNCTION subscribe

$client->subscribe( $channel );

FUNCTION on

$client->on( 'connect', sub { my(%data) = @_; ... });

Known events are 'message', 'connect', 'disconnect', 'subscribe', 'unsubscribe', 'publish', 'presence', 'history', 'join', 'leave', 'refresh', 'ping'

FUNCTION client_id

$client->client_id() return the client_id if it is connected to Centrifugo, or undef.