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
87 stars 42 forks source link

Problem to subscribe #143

Open arwace opened 1 year ago

arwace commented 1 year ago

Hello?

I'm having trouble subscribing. When I use redisClusterCommand, I get an error saying that I don't have a key and shouldn't do it, When using redisClusterCommandToNode, the SUBSCRIBE command works fine, but I can't get the publish data with redisClusterGetReply. What should I do in this case?

zuiderkwast commented 1 year ago

Hi! Subscribe is not yet supported by hiredis-cluster. We want to fix it in the future. See #27.

One possibility to get the published data is to use hiredis directly. You can get the hiredis context from hiredis-cluster like this:

/* Get a node. */
nodeIterator iter;
initNodeIterator(&iter, cc);
redisClusterNode *node = nodeNext(&iter);
/* Another way is
 * node = redisClusterGetNodeByKey(cc, "some key");
 */

/* Get the hiredis context, connect if needed */
redisContext *c = ctx_get_by_node(cc, node);

/* Use hiredis functions with context c,
 * for example loop to get all pubsub messages */
redisReply *reply = redisCommand(c, "SUBSCRIBE foo");
freeReplyObject(reply);
while (redisGetReply(c, (void *)&reply) == REDIS_OK) {
    // consume message
    freeReplyObject(reply);
}