Kelp::Module::WebSocket::AnyEvent - AnyEvent websocket server integration with Kelp
# in config modules => [qw(Symbiosis WebSocket::AnyEvent)], modules_init => { "WebSocket::AnyEvent" => { serializer => "json", }, }, # in application's build method my $ws = $self->websocket; $ws->add(message => sub { my ($conn, $msg) = @_; $conn->send({received => $msg}); }); $self->symbiosis->mount("/ws" => $ws); # in psgi script $app = MyApp->new; $app->run_all;
This is a module that integrates a websocket instance into Kelp using Kelp::Module::Symbiosis. To run it, a non-blocking Plack server based on AnyEvent is required, like Twiggy. All this module does is wrap Plack::App::WebSocket instance in Kelp's module, introduce a method to get this instance in Kelp and integrate it into running alongside Kelp using Symbiosis. An instance of this class will be available in Kelp under the websocket method.
sig: connections($self)
Returns a hashref containing all available Kelp::Module::WebSocket::AnyEvent::Connection instances (open connections) keyed by their unique id. An id is autoincremented from 1 and guaranteed not to change and not to be replaced by a different connection unless the server restarts.
sig: middleware($self)
Returns an arrayref of all middlewares in format: [ middleware_class, [ middleware_config ] ].
[ middleware_class, [ middleware_config ] ]
sig: psgi($self)
Returns a ran instance of Plack::App::WebSocket.
sig: run($self)
Same as psgi, but wraps the instance in all wanted middlewares.
sig: add($self, $event, $handler)
Registers a $handler (coderef) for websocket $event (string). Handler will be passed an instance of Kelp::Module::WebSocket::AnyEvent::Connection and an incoming message. $event can be either one of: open close message error. You can only specify one handler for each event type.
Dancer2::Plugin::WebSocket, same integration for Dancer2 framework this module was inspired by
Kelp, the framework
Twiggy, a server capable of running this websocket
Bartosz Jarzyna, <brtastic.dev@gmail.com>
Copyright (C) 2020 by Bartosz Jarzyna
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.
To install Kelp::Module::WebSocket::AnyEvent, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Kelp::Module::WebSocket::AnyEvent
CPAN shell
perl -MCPAN -e shell install Kelp::Module::WebSocket::AnyEvent
For more information on module installation, please visit the detailed CPAN module installation guide.