Nordix / hiredis-cluster

C client library for Valkey/Redis Cluster. This project is used and sponsored by Ericsson. It is a fork of the now unmaintained hiredis-vip.
BSD 3-Clause "New" or "Revised" License
86 stars 42 forks source link

Does hiredis-cluster support SSUBSCRIBE? #166

Open arwace opened 1 year ago

arwace commented 1 year ago

Neither synchronous nor asynchronous mode can handle SSUBSCRIBE.

Is there an example that handles SSUBSCRIBE?

zuiderkwast commented 1 year ago

Hi @arwace!

No, unfortunately hiredis-cluster does not support any kind of subscribe (SUBSCRIBE, PSUBSCRIBE, SSUBSCRIBE). See #27.

Regarding SSUBSCRIBE, not even the underlying hiredis supports it. See https://github.com/redis/hiredis/pull/1066.

redis-cli (which comes with redis) can support ssubscribe and it uses hiredis, but it does it's own handling. (It calls redisGetReply() repeatedly and checks if each reply looks like a pushed pubsub message.)

zuiderkwast commented 1 year ago

If you have a blocking thread to consume messages, one possible workaround is to use hiredis directly. You can get the hiredis context from hiredis-cluster like this:

redisClusterNode *node = redisClusterGetNodeByKey(cc, "channel");
redisContext *c = ctx_get_by_node(cc, node);

Then use hiredis functions with context c, for example to get all pubsub messages in a loop.

redisReply *reply = redisCommand(c, "SSUBSCRIBE channel");
freeReplyObject(reply);
while (redisGetReply(c, (void *)&reply) == REDIS_OK) {
    // consume message
    freeReplyObject(reply);
}