Mojolicious::Plugin::Pubsub - Pubsub plugin for Mojolicious
version 0.006
# Mojolicious my $pubsub = $app->plugin('Pubsub', { cb => sub { print "Message: $_[0]\n"; }, socket => 'myapp.pubsub', }); $app->pubsub->publish("message"); # Mojolicious::Lite my $pubsub = plugin Pubsub => { cb => sub { print "Message: $_[0]\n"; }, socket => 'myapp.pubsub', }; app->pubsub->publish("message");
Easy way to add pubsub to your Mojolicious apps; it hooks into the Mojo::IOLoop to send and receive messages asynchronously.
Each time you run your Mojolicious app and the plugin gets loaded, it'll spawn a new daemon that'll try to connect to the socket if it already exists, and if it fails it will replace the socket assuming that the underlying daemon is dead. If it succeeds, it will cancel the new daemon and leave the old one to continue doing its work.
Note: MSWin32 is not supported because it has no proper UNIX socket support.
UNIX
Takes a callback CODE reference.
CODE
A path to a UNIX socket used to communicate between the publishers. By default this will be $app->home->child($app->moniker . '.pubsub').
$app->home->child($app->moniker . '.pubsub')
$c->pubsub->publish("message"); $c->pubsub->publish(@args);
Publishes a message that the subscribing callbacks will receive.
$c->pubsub->subscribe($cb);
Add the $cb code reference to the callbacks that get published messages.
$cb
$c->pubsub->unsubscribe($cb);
Remove the $cb code reference from the callbacks that get published messages.
my $subscriber = sub { my @args = @_; ... };
Subscribers sent to the cb option, or the pubsub->subscribe helper should simply be CODE references that handle the arguments passed in. The @args will be the same as what was passed in to the pubsub->publish helper, except they will have gotten JSON encoded via Mojo::JSON on the way, so only data structures that consist of regular scalars, arrays, hashes, and objects that implement TO_JSON or that stringify will work correctly. See Mojo::JSON for more details.
cb
pubsub->subscribe
@args
pubsub->publish
JSON
TO_JSON
my $pubsub = $plugin->register(Mojolicious->new, { cb => sub { ... }, socket => $path });
Register plugin in Mojolicious application.
Mojolicious, Mojo::Redis2.
Andreas Guldstrand <andreas.guldstrand@gmail.com>
This software is Copyright (c) 2018 by Andreas Guldstrand.
This is free software, licensed under:
The MIT (X11) License
To install Mojolicious::Plugin::Pubsub, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Pubsub
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Pubsub
For more information on module installation, please visit the detailed CPAN module installation guide.