redishiredis

Hiredis waiting for message


I am using hiredis C library to connect to redis server. I am not able to figure out how to wait for new messages after subscribing to new message.

My code look like:

signal(SIGPIPE, SIG_IGN );
  struct event_base *base = event_base_new();

  redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
  if (c->err) {
    /* Let *c leak for now... */
    printf("Error: %s\n", c->errstr);
    return 1;
  }

  redisLibeventAttach(c, base);
  redisAsyncSetConnectCallback(c, connectCallback);
  redisAsyncSetDisconnectCallback(c, disconnectCallback);
  redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc - 1],
                    strlen(argv[argc - 1]));
  redisAsyncCommand(c, getCallback, (char*) "end-1", "GET key");
  redisAsyncCommand(c, getCallback, (char*) "end-1", "SUBSCRIBE foo");

Now how to tell hiredis to wait for message on channel ?


Solution

  • You do not have to tell hiredis you need to wait on channel: the event loop will just wait on the Redis connection which has been previously registered.

    Here is a complete example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <signal.h>
    #include "hiredis.h"
    #include "async.h"
    #include "adapters/libevent.h"
    
    void subCallback(redisAsyncContext *c, void *r, void *priv) {
        redisReply *reply = r;
        if (reply == NULL) return;
        if ( reply->type == REDIS_REPLY_ARRAY && reply->elements == 3 ) {
            if ( strcmp( reply->element[0]->str, "subscribe" ) != 0 ) {
                printf( "Received[%s] channel %s: %s\n",
                        (char*)priv,
                        reply->element[1]->str,
                        reply->element[2]->str );
            }
        }
    }
    
    void connectCallback(const redisAsyncContext *c, int status) {
        if (status != REDIS_OK) {
            printf("Error: %s\n", c->errstr);
            return;
        }
        printf("Connected...\n");
    }
    
    void disconnectCallback(const redisAsyncContext *c, int status) {
        if (status != REDIS_OK) {
            printf("Error: %s\n", c->errstr);
            return;
        }
        printf("Disconnected...\n");
    }
    
    int main (int argc, char **argv) {
        signal(SIGPIPE, SIG_IGN);
        struct event_base *base = event_base_new();
    
        redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
        if (c->err) {
            /* Let *c leak for now... */
            printf("Error: %s\n", c->errstr);
            return 1;
        }
    
        redisLibeventAttach(c,base);
        redisAsyncSetConnectCallback(c,connectCallback);
        redisAsyncSetDisconnectCallback(c,disconnectCallback);
        redisAsyncCommand(c, subCallback, (char*) "sub", "SUBSCRIBE foo");
    
        event_base_dispatch(base);
        return 0;
    }
    

    You can test it by just use the following command to publish something:

    redis-cli publish foo something
    

    The event_base_dispatch function is the one which actually launches the event loop, and makes it wait on Redis subscription.