Mojo::Pg::PubSub - Publish/Subscribe
use Mojo::Pg::PubSub; my $pubsub = Mojo::Pg::PubSub->new(pg => $pg); my $cb = $pubsub->listen(foo => sub { my ($pubsub, $payload) = @_; say "Received: $payload"; }); $pubsub->notify(foo => 'bar'); $pubsub->unlisten(foo => $cb);
Mojo::Pg::PubSub is a scalable implementation of the publish/subscribe pattern used by Mojo::Pg. It is based on PostgreSQL notifications and allows many consumers to share the same database connection, to avoid many common scalability problems.
Mojo::Pg::PubSub inherits all events from Mojo::EventEmitter and can emit the following new ones.
$pubsub->on(reconnect => sub { my ($pubsub, $db) = @_; ... });
Emitted after switching to a new database connection for sending and receiving notifications.
Mojo::Pg::PubSub implements the following attributes.
my $pg = $pubsub->pg; $pubsub = $pubsub->pg(Mojo::Pg->new);
Mojo::Pg object this publish/subscribe container belongs to.
Mojo::Pg::PubSub inherits all methods from Mojo::EventEmitter and implements the following new ones.
my $cb = $pubsub->listen(foo => sub {...});
Subscribe to a channel, there is no limit on how many subscribers a channel can have.
# Subscribe to the same channel twice $pubsub->listen(foo => sub { my ($pubsub, $payload) = @_; say "One: $payload"; }); $pubsub->listen(foo => sub { my ($pubsub, $payload) = @_; say "Two: $payload"; });
$pubsub = $pubsub->notify('foo'); $pubsub = $pubsub->notify(foo => 'bar');
Notify a channel.
$pubsub = $pubsub->unlisten('foo'); $pubsub = $pubsub->unlisten(foo => $cb);
Unsubscribe from a channel.
Mojo::Pg, Mojolicious::Guides, http://mojolicious.org.
To install Mojo::Pg, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Pg
CPAN shell
perl -MCPAN -e shell install Mojo::Pg
For more information on module installation, please visit the detailed CPAN module installation guide.