Created
February 27, 2013 04:56
-
-
Save ox/5045199 to your computer and use it in GitHub Desktop.
An example of a C program using hiredis to SUBSCRIBE to a Redis channel. It listens for messages and prints them out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <hiredis/hiredis.h> | |
#include <hiredis/async.h> | |
#include <hiredis/adapters/libevent.h> | |
void onMessage(redisAsyncContext * c, void *reply, void * privdata) { | |
int j; | |
redisReply * r = reply; | |
if (reply == NULL) return; | |
printf("got a message of type: %i\n", r->type); | |
if (r->type == REDIS_REPLY_ARRAY) { | |
for (j = 0; j < r->elements; j++) { | |
printf("%u) %s\n", j, r->element[j]->str); | |
} | |
} | |
} | |
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) { | |
printf("error: %s\n", c->errstr); | |
return 1; | |
} | |
redisLibeventAttach(c, base); | |
redisAsyncCommand(c, onMessage, NULL, "SUBSCRIBE test_channel"); | |
event_base_dispatch(base); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment