1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
use crate::{
commands,
error::RedisError,
interfaces::{ClientLike, RedisResult},
types::{FromRedis, KeyspaceEvent, Message, MultipleStrings, RedisValue},
};
use bytes_utils::Str;
use std::convert::TryInto;
use tokio::sync::broadcast::Receiver as BroadcastReceiver;
/// Functions that implement the [pubsub](https://redis.io/commands#pubsub) interface.
#[async_trait]
pub trait PubsubInterface: ClientLike + Sized {
/// Listen for messages on the publish-subscribe interface.
///
/// **Keyspace events are not sent on this interface.**
///
/// If the connection to the Redis server closes for any reason this function does not need to be called again.
/// Messages will start appearing on the original stream after [subscribe](Self::subscribe) is called again.
fn on_message(&self) -> BroadcastReceiver<Message> {
self.inner().notifications.pubsub.load().subscribe()
}
/// Listen for keyspace and keyevent notifications on the publish-subscribe interface.
///
/// Callers still need to configure the server and subscribe to the relevant channels, but this interface will
/// parse and format the messages automatically.
///
/// If the connection to the Redis server closes for any reason this function does not need to be called again.
///
/// <https://redis.io/topics/notifications>
fn on_keyspace_event(&self) -> BroadcastReceiver<KeyspaceEvent> {
self.inner().notifications.keyspace.load().subscribe()
}
/// Subscribe to a channel on the publish-subscribe interface.
///
/// <https://redis.io/commands/subscribe>
async fn subscribe<R, S>(&self, channels: S) -> RedisResult<R>
where
R: FromRedis,
S: Into<MultipleStrings> + Send,
{
into!(channels);
commands::pubsub::subscribe(self, channels).await?.convert()
}
/// Unsubscribe from a channel on the PubSub interface.
///
/// <https://redis.io/commands/unsubscribe>
async fn unsubscribe<S>(&self, channels: S) -> RedisResult<()>
where
S: Into<MultipleStrings> + Send,
{
into!(channels);
commands::pubsub::unsubscribe(self, channels).await?.convert()
}
/// Subscribes the client to the given patterns.
///
/// <https://redis.io/commands/psubscribe>
async fn psubscribe<R, S>(&self, patterns: S) -> RedisResult<R>
where
R: FromRedis,
S: Into<MultipleStrings> + Send,
{
into!(patterns);
commands::pubsub::psubscribe(self, patterns).await?.convert()
}
/// Unsubscribes the client from the given patterns, or from all of them if none is given.
///
/// If no channels are provided this command returns an empty array.
///
/// <https://redis.io/commands/punsubscribe>
async fn punsubscribe<S>(&self, patterns: S) -> RedisResult<()>
where
S: Into<MultipleStrings> + Send,
{
into!(patterns);
commands::pubsub::punsubscribe(self, patterns).await?.convert()
}
/// Publish a message on the PubSub interface, returning the number of clients that received the message.
///
/// <https://redis.io/commands/publish>
async fn publish<R, S, V>(&self, channel: S, message: V) -> RedisResult<R>
where
R: FromRedis,
S: Into<Str> + Send,
V: TryInto<RedisValue> + Send,
V::Error: Into<RedisError> + Send,
{
into!(channel);
try_into!(message);
commands::pubsub::publish(self, channel, message).await?.convert()
}
/// Subscribes the client to the specified shard channels.
///
/// <https://redis.io/commands/ssubscribe/>
async fn ssubscribe<R, C>(&self, channels: C) -> RedisResult<R>
where
R: FromRedis,
C: Into<MultipleStrings> + Send,
{
into!(channels);
commands::pubsub::ssubscribe(self, channels).await?.convert()
}
/// Unsubscribes the client from the given shard channels, or from all of them if none is given.
///
/// If no channels are provided this command returns an empty array.
///
/// <https://redis.io/commands/sunsubscribe/>
async fn sunsubscribe<C>(&self, channels: C) -> RedisResult<()>
where
C: Into<MultipleStrings> + Send,
{
into!(channels);
commands::pubsub::sunsubscribe(self, channels).await?.convert()
}
/// Posts a message to the given shard channel.
///
/// <https://redis.io/commands/spublish/>
async fn spublish<R, S, V>(&self, channel: S, message: V) -> RedisResult<R>
where
R: FromRedis,
S: Into<Str> + Send,
V: TryInto<RedisValue> + Send,
V::Error: Into<RedisError> + Send,
{
into!(channel);
try_into!(message);
commands::pubsub::spublish(self, channel, message).await?.convert()
}
/// Lists the currently active channels.
///
/// <https://redis.io/commands/pubsub-channels/>
async fn pubsub_channels<R, S>(&self, pattern: S) -> RedisResult<R>
where
R: FromRedis,
S: Into<Str> + Send,
{
into!(pattern);
commands::pubsub::pubsub_channels(self, pattern).await?.convert()
}
/// Returns the number of unique patterns that are subscribed to by clients.
///
/// <https://redis.io/commands/pubsub-numpat/>
async fn pubsub_numpat<R>(&self) -> RedisResult<R>
where
R: FromRedis,
{
commands::pubsub::pubsub_numpat(self).await?.convert()
}
/// Returns the number of subscribers (exclusive of clients subscribed to patterns) for the specified channels.
///
/// <https://redis.io/commands/pubsub-numsub/>
async fn pubsub_numsub<R, S>(&self, channels: S) -> RedisResult<R>
where
R: FromRedis,
S: Into<MultipleStrings> + Send,
{
into!(channels);
commands::pubsub::pubsub_numsub(self, channels).await?.convert()
}
/// Lists the currently active shard channels.
///
/// <https://redis.io/commands/pubsub-shardchannels/>
async fn pubsub_shardchannels<R, S>(&self, pattern: S) -> RedisResult<R>
where
R: FromRedis,
S: Into<Str> + Send,
{
into!(pattern);
commands::pubsub::pubsub_shardchannels(self, pattern).await?.convert()
}
/// Returns the number of subscribers for the specified shard channels.
///
/// <https://redis.io/commands/pubsub-shardnumsub/>
async fn pubsub_shardnumsub<R, S>(&self, channels: S) -> RedisResult<R>
where
R: FromRedis,
S: Into<MultipleStrings> + Send,
{
into!(channels);
commands::pubsub::pubsub_shardnumsub(self, channels).await?.convert()
}
}