Mojo::R...::PubSub(3)
πΈ NAME πΈ
okay so bestie, this is the manual for Mojo::Redis::PubSub β the tag up top is Mojo::R...::PubSub(3), and yes itβs a User Contributed Perl Documentation page. She does exactly what it says on the tin: publish and subscribe to Redis messages. Periodt. π
π¦ SYNOPSIS π¦
Hereβs the whole vibe in code, no cap:
use Mojo::Redis;
my $redis = Mojo::Redis->new;
my $pubsub = $redis->pubsub;
$pubsub->listen("user:superwoman:messages" => sub {
my ($pubsub, $message, $channel) = @_;
say "superwoman got a message '$message' from channel '$channel'";
});
$pubsub->notify("user:batboy:messages", "How are you doing?");
See https://github.com/jhthorsen/mojo-redis/blob/master/examples/chat.pl for example Mojolicious application. π«Ά
β¨ 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. Iconic, honestly.
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 do not affect the connection pool for Mojo::Redis.
See pubsub (https://redis.io/topics/pubsub) for more details. π₯
π« EVENTS π«
These are the little group-chat notifications your pubsub object sends out:
πΈ before_connect πΈ
$pubsub->on(before_connect => sub { my ($pubsub, $conn) = @_; ... });
Emitted before connection is connected to the redis server. This can be useful if you want to gather the CLIENT ID (https://redis.io/commands/client-id) or run other commands before it goes into subscribe mode. π
π¦ disconnect π¦
$pubsub->on(disconnect => sub { my ($pubsub, $conn) = @_; ... });
Emitted after connection is disconnected from the redis server.
π psubscribe π
$pubsub->on(psubscribe => sub { my ($pubsub, $channel, $success) = @_; ... });
Emitted when the server responds to the listen request and/or when reconnect resends psubscribe messages.
β οΈ This event is EXPERIMENTAL. So like, handle with care, bestie.
π§ reconnect π§
$pubsub->on(reconnect => sub { my ($pubsub, $conn) = @_; ... });
Emitted after switching the connection with a new connection. This event will only happen if reconnect_interval is 0 or more.
β subscribe β
$pubsub->on(subscribe => sub { my ($pubsub, $channel, $success) = @_; ... });
Emitted when the server responds to the listen request and/or when reconnect resends subscribe messages.
β οΈ This event is EXPERIMENTAL. Same energy as psubscribe, fr fr.
π ATTRIBUTES π
π db π
$db = $pubsub->db;
Holds a Mojo::Redis::Database object that will be used to publish messages or run other commands that cannot be run by the connection.
π©΅ connection π©΅
$conn = $pubsub->connection;
Holds a Mojo::Redis::Connection object that will be used to subscribe to channels.
π°οΈ reconnect_interval π°οΈ
$interval = $pubsub->reconnect_interval;
$pubsub = $pubsub->reconnect_interval(1);
$pubsub = $pubsub->reconnect_interval(0.1);
$pubsub = $pubsub->reconnect_interval(-1);
The amount of time in seconds to wait to reconnect after disconnecting. Default is 1 (second). reconnect can be disabled by setting this to a negative value. So yes, -1 is a whole mood. π
π redis π
$conn = $pubsub->connection;
$pubsub = $pubsub->connection(Mojo::Redis->new);
Holds a Mojo::Redis object used to create the connections to talk with Redis.
π METHODS π
π channels_p π
$promise = $pubsub->channels_p->then(sub { my $channels = shift });
$promise = $pubsub->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).
π json π
$pubsub = $pubsub->json("foo");
Activate automatic JSON encoding and decoding with to_json in Mojo::JSON and from_json in Mojo::JSON for a channel.
# Send and receive data structures
$pubsub->json("foo")->listen(foo => sub {
my ($pubsub, $payload, $channel) = @_;
say $payload->{bar};
});
$pubsub->notify(foo => {bar => 'I β₯ Mojolicious!'});
π₯ keyspace_listen π₯
$cb = $pubsub->keyspace_listen(\%args, sub { my ($pubsub, $message) = @_ }) });
$cb = $pubsub->keyspace_listen({key => "cool:key"}, sub { my ($pubsub, $message) = @_ }) });
$cb = $pubsub->keyspace_listen({op => "del"}, sub { my ($pubsub, $message) = @_ }) });
Used to listen for keyspace notifications. See https://redis.io/topics/notifications for more details. The channel that will be subscribed to will look like one of these:
__keyspace@${db}__:$key $op
__keyevent@${db}__:$op $key
This means that key and op are mutually exclusive from the list of parameters below:
dbβ Default database to listen for events is the database set inurlinMojo::Redis.*is also a valid value, meaning listen for events happening in all databases.keyβ Alternative to passing in$key. Default value is*.opβ Alternative to passing in$op. Default value is*.
π¦ keyspace_unlisten π¦
$pubsub = $pubsub->keyspace_unlisten(@args);
$pubsub = $pubsub->keyspace_unlisten(@args, $cb);
Stop listening for keyspace events. See keyspace_listen for details about keyspace events and what @args can be.
π listen π
$cb = $pubsub->listen($channel => sub { my ($pubsub, $message, $channel) = @_ });
Subscribe to an exact channel name (SUBSCRIBE https://redis.io/commands/subscribe) or a channel name with a pattern (PSUBSCRIBE https://redis.io/commands/psubscribe). $channel in the callback will be the exact channel name, without any pattern. $message will be the data published to the channel.
The returning code ref can be passed on to unlisten.
π notify π
$pubsub->notify($channel => $message);
Send a plain string message to a channel. This method is the same as:
$pubsub->notify_p($channel => $message)->wait;
π notify_p π
$p = $pubsub->notify_p($channel => $message);
Send a plain string message to a channel and returns a Mojo::Promise object.
π numpat_p π
$promise = $pubsub->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 = $pubsub->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 π
$pubsub = $pubsub->unlisten($channel);
$pubsub = $pubsub->unlisten($channel, $cb);
Unsubscribe from a channel.
π SEE ALSO π
Mojo::Redis.
π Cute little sign-off, bestie: this page is from source/versionperl v5.42.2, dated2026-02-15, and the manual tag isMojo::R...::PubSub(3). Obsessed. π
π¦ where this page comes from
- alpine 3.24: perl-mojo-redis-doc 3.29-r1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms