Skip to content

Instantly share code, notes, and snippets.

@davidszotten
Created August 7, 2014 16:32
Show Gist options
  • Select an option

  • Save davidszotten/549b5306f2b34a205075 to your computer and use it in GitHub Desktop.

Select an option

Save davidszotten/549b5306f2b34a205075 to your computer and use it in GitHub Desktop.
redis c pubsub
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis.h>
int main(int argc, char **argv) {
unsigned int j;
redisContext *conn;
redisReply *reply;
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
int port = (argc > 2) ? atoi(argv[2]) : 6379;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
conn = redisConnectWithTimeout(hostname, port, timeout);
if (conn == NULL || conn->err) {
if (conn) {
printf("Connection error: %s\n", conn->errstr);
redisFree(conn);
} else {
printf("Connection error: can't allocate redis context\n");
}
exit(1);
}
reply = redisCommand(conn,"PUBLISH foo hello");
printf("PUBLISH: %s\n", reply->str);
freeReplyObject(reply);
/* Disconnects and frees the context */
redisFree(conn);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment