NAME

Mojo::Redis::PubSub - Publish and subscribe to Redis messages

SYNOPSIS

use Mojo::Redis;

my $redis  = Mojo::Redis->new;
my $pubsub = $redis->pubsub;

$pubsub->listen("user:superwoman:messages" => sub {
  my ($pubsub, $message) = @_;
  say "superwoman got a message: $message";
});

$pubsub->notify("user:batboy:messages", "How are you doing?");

DESCRIPTION

Mojo::Redis::PubSub is an implementation of the Redis Publish/Subscribe messaging paradigm. This class has the same API as Mojo::Pg::PubSub, so you can easily switch between the backends.

This object holds one connection for receiving messages, and one connection for sending messages. They are created lazily the first time "listen" or "notify" is called. These connections does not affect the connection pool for Mojo::Redis.

See pubsub for more details.

ATTRIBUTES

connection

$conn = $self->connection;
$self = $self->connection(Mojo::Redis::Connection->new);

Holds a Mojo::Redis::Connection object.

redis

$conn = $self->connection;
$self = $self->connection(Mojo::Redis::Connection->new);

Holds a Mojo::Redis object used to create the connections to talk with Redis.

METHODS

channels_p

$promise = $self->channels_p->then(sub { my $channels = shift });
$promise = $self->channels_p("pat*")->then(sub { my $channels = shift });

Lists the currently active channels. An active channel is a Pub/Sub channel with one or more subscribers (not including clients subscribed to patterns).

listen

$cb = $self->listen($channel => sub { my ($self, $message) = @_ });

Subscribe to a channel, there is no limit on how many subscribers a channel can have. The returning code ref can be passed on to "unlisten".

notify

$self->notify($channel => $message);

Send a plain string message to a channel.

numpat_p

$promise = $self->channels_p->then(sub { my $int = shift });

Returns the number of subscriptions to patterns (that are performed using the PSUBSCRIBE command). Note that this is not just the count of clients subscribed to patterns but the total number of patterns all the clients are subscribed to.

numsub_p

$promise = $self->numsub_p(@channels)->then(sub { my $channels = shift });

Returns the number of subscribers (not counting clients subscribed to patterns) for the specified channels as a hash-ref, where the keys are channel names.

unlisten

$self = $self->unlisten($channel);
$self = $self->unlisten($channel, $cb);

Unsubscribe from a channel.

SEE ALSO

Mojo::Redis.