1

I have been working with hiredis as a client, everything e.g set, publish, etc works except for subscribe. I'm able to subscribe but can't get any message when I try to publish to the channel I've subscribed to. Here is the code I'm using:

int main() {
    signal(SIGPIPE, sigHandler);
    struct event_base *base = event_base_new();

    assert(base != nullptr);

    auto redis = redisAsyncConnect(IP, PORT);

    if (redis != nullptr && redis->err) {
        std::cerr << redis->err;
        delete redis;
        exit(1);
    }
    std::cout << "Connected" << END;

    int flag;

    std::cout << "Calling redisLibeventAttach" << END;

    redisLibeventAttach(redis, base);
    flag = redisAsyncCommand(redis, onMessage, (char *) "sub", "SUBSCRIBE test");

    std::cout << "Command ret: " << flag << END;

    flag = event_base_dispatch(base);
    std::cout << " Event base flag " << flag << END;


    return 0;
}

And the on message function:

void onMessage(redisAsyncContext *context, void *r, void *data) {

    assert(r != nullptr);
    auto *reply = static_cast<redisReply *>(r);

    assert(reply != nullptr);

    std::cout << "Reply " << reply->type << END;

    switch (reply->type) {
        case REDIS_REPLY_ARRAY:

            for (int i = 0; i < reply->elements; ++i) {
                std::cout << i << " -- " << reply->element[i]->str << END;
            }

            break;
        case REDIS_REPLY_STRING:
            std::cout << reply->str << END;
            break;
    }


}

Is there anything else I'm supposed to do?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Wafula Samuel
  • 530
  • 1
  • 8
  • 25

0 Answers0