|
NAMEPocketIO - Socket.IO PSGI applicationSYNOPSISuse 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; }; DESCRIPTIONPocketIO is a server implementation of SocketIO in Perl, you still need "socket.io" javascript library on the client.PocketIO aims to have API as close as possible to the Node.js implementation and sometimes it might look not very perlish. How to useFirst 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.When the client is connected your handler is called with a PocketIO::Socket object as a first parameter. Sending and receiving messagesA simple echo handler can look like this:sub { my $self = shift; $self->on('message' => sub { my $self = shift; my ($message) = @_; $self->send($message); }); } Sending and receiving eventsEvents 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 and sending messages/events to everybodyBroadcasting is sending messages to everybody except you:$self->broadcast->send('foo'); $self->broadcast->emit('foo'); Method "sockets" represents all connected clients: $self->sockets->send('foo'); $self->sockets->emit('foo'); AcknowlegementsSometimes 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'}); Storing data in the socket objectOften 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) = @_; }); } NamespacingNot implemented yet.Volatile messagesNot implemented yet.RoomsA room is a named group of connections for more fine-grained broadcasts. You can subscribe or unsubscribe a socket to/from a room:sub { my $self = shift; $self->join('a room'); $self->sockets->in('a room')->emit('message', data); $self->broadcast->to('a room')->emit("other message"); } CONFIGURATIONS
TLS/SSLFor TLS/SSL a secure proxy is needed. "stunnel" or App::TLSMe are recommended.SCALINGSee PocketIO::Pool::Redis.DEBUGGINGUse "POCKETIO_DEBUG" and "POCKETIO_CONNECTION_DEBUG" variables for debugging.METHODS"new"Create a new PocketIO instance."pool"Holds PocketIO::Pool object by default."call"For Plack apps compatibility."to_app"Returns PSGI code reference.SEE ALSOMore 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 DEVELOPMENTRepositoryhttp://github.com/vti/pocketio CREDITSSocket.IO author(s) and contributors.Jens Gassmann Uwe Voelker Oskari Okko Ojala Jason May Michael FiG Peter Stuifzand tokubass mvgrimes AUTHORViacheslav Tykhanovskyi, "vti@cpan.org".COPYRIGHT AND LICENSECopyright (C) 2011-2013, Viacheslav TykhanovskyiThis program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Visit the GSP FreeBSD Man Page Interface. |