PocketIO - Socket.IO PSGI application
use Plack::Builder; builder { mount '/socket.io' => PocketIO->new( handler => sub { my $self = shift; $self->on( 'message' => sub { my $self = shift; my ($message) = @_; ...; } ); $self->send({buffer => []}); } ); $app; }; # or builder { mount '/socket.io' => PocketIO->new(class => 'MyApp::Handler', method => 'run'); $app; };
PocketIO is a server implementation of SocketIO in Perl, you still need socket.io javascript library on the client.
socket.io
PocketIO aims to have API as close as possible to the Node.js implementation and sometimes it might look not very perlish.
First you mount PocketIO as a normal Plack application. It is recommended to mount it to the /socket.io path since that will not require any changes on the client side.
/socket.io
When the client is connected your handler is called with a PocketIO::Socket object as a first parameter.
A simple echo handler can look like this:
sub { my $self = shift; $self->on('message' => sub { my $self = shift; my ($message) = @_; $self->send($message); }); }
Events are special messages that behave like rpc calls.
sub { my $self = shift; $self->on('username' => sub { my $self = shift; my ($nick) = @_; ... }); $self->emit('username', 'vti'); }
Broadcasting is sending messages to everybody except you:
$self->broadcast->send('foo'); $self->broadcast->emit('foo');
Method sockets represents all connected clients:
sockets
$self->sockets->send('foo'); $self->sockets->emit('foo');
Sometimes you want to know when the client received a message or event. In order to achieve this just pass a callback as the last parameter:
$self->send('foo', sub {'client got message'}); $self->emit('foo', sub {'client got event'});
Often it is required to store some data in the client object. Instead of using global variables there are two handy methods:
sub { my $self = shift; $self->set(foo => 'bar', sub { 'ready' }); $self->get('foo' => sub { my $self = shift; my ($err, $foo) = @_; }); }
Not implemented yet.
PocketIO->new( handler => sub { my $socket = shift; $socket->on( 'message' => sub { my $socket = shift; } ); $socket->send('hello'); } );
PocketIO->new(class => 'MyHandler', method => 'run'); # or PocketIO->new(instance => MyHandler->new(foo => 'bar'), method => 'run'); package MyHandler; sub new { ... } # or use Moose, Boose, Goose, Doose sub run { my $self = shift; return sub { # same code as above } }
Loads class, creates a new object or uses a passed instance and runs run method expecting it to return an anonymous subroutine.
class
instance
run
For TLS/SSL a secure proxy is needed. stunnel or App::TLSMe are recommended.
stunnel
See PocketIO::Pool::Redis.
Use POCKETIO_DEBUG and POCKETIO_CONNECTION_DEBUG variables for debugging.
POCKETIO_DEBUG
POCKETIO_CONNECTION_DEBUG
new
Create a new PocketIO instance.
pool
Holds PocketIO::Pool object by default.
call
For Plack apps compatibility.
to_app
Returns PSGI code reference.
More information about SocketIO you can find on the website http://socket.io/, or on the GitHub https://github.com/LearnBoost/Socket.IO.
Protocol::SocketIO, PSGI
http://github.com/vti/pocketio
Socket.IO author(s) and contributors.
Jens Gassmann
Uwe Voelker
Oskari Okko Ojala
Viacheslav Tykhanovskyi, vti@cpan.org.
vti@cpan.org
Copyright (C) 2011-2012, Viacheslav Tykhanovskyi
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
To install PocketIO, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PocketIO
CPAN shell
perl -MCPAN -e shell install PocketIO
For more information on module installation, please visit the detailed CPAN module installation guide.