{
  "schema_version": 2,
  "id": "develop/clients/hiredis/int-examples/libevent-integration",
  "title": "Integrate hiredis with a libevent app",
  "url": "https://redis.io/docs/latest/develop/clients/hiredis/int-examples/libevent-integration/",
  "summary": "Use `hiredis` in conjunction with the `libevent` framework.",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-09T11:06:49+01:00",
  "page_type": "content",
  "content_hash": "64747af2bf9666eb1d60297cc46c36f65aaba41009cf788aa34c34b768b3b541",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "The [`libevent`](https://libevent.org/) library provides an\nimplementation of an event loop that lets you call functions\nasynchronously in response to events. This guide explains\nhow to use `hiredis` to connect to a Redis server from a\n`libevent` app."
    },
    {
      "id": "install-libevent",
      "title": "Install `libevent`",
      "role": "setup",
      "text": "The [`libevent` home page](https://libevent.org/) has links to download\nall versions of the library, but you should use the latest version\nunless there is a specific version you need to target.\n\nWhen you have downloaded `libevent`, follow the instructions in the\n`README` file to compile and install the library."
    },
    {
      "id": "create-a-simple-app",
      "title": "Create a simple app",
      "role": "content",
      "text": "For a real project, you would build your code with a makefile, but for\nthis simple test, you can just place it in a file called `main.c` and\nbuild it with the following command (assuming you used `make install` to\ninstall the `libhiredis` and `libevent` libraries):\n\n[code example]\n\nSee [Build and install](https://redis.io/docs/latest/develop/clients/hiredis#build-and-install)\nto learn how to build `hiredis`, if you have not already done so.\n\nNow, add the following code in `main.c`. An explanation follows the\ncode example:\n\n[code example]\n\nThe code calls\n[`event_base_new()`](https://libevent.org/doc/event_8h.html#af34c025430d445427a2a5661082405c3)\nto initialize the core\n[`event_base`](https://libevent.org/doc/structevent__base.html)\nobject that manages the event loop. It then creates a standard\n[asynchronous connection](https://redis.io/docs/latest/develop/clients/hiredis/connect#asynchronous-connection)\nto Redis and uses the `libevent` adapter function `redisLibeventAttach()` to\nattach the connection to the event loop.\n\nAfter setting the [connection callbacks](https://redis.io/docs/latest/develop/clients/hiredis/connect#asynchronous-connection), the code issues two asynchronous\nRedis commands (see\n[Construct asynchronous commands](https://redis.io/docs/latest/develop/clients/hiredis/issue-commands#construct-asynchronous-commands)\nfor more information).\nThe final step is to call\n[`event_base_dispatch()`](https://libevent.org/doc/event_8h.html#a19d60cb72a1af398247f40e92cf07056)\nto start the event loop. This will wait for the commands to be processed and\nthen exit when the Redis connection is closed in the `getCallback()` function."
    },
    {
      "id": "run-the-code",
      "title": "Run the code",
      "role": "content",
      "text": "If you compile and run the code, you will see the following output,\nshowing that the callbacks executed correctly:\n\n[code example]\n\nYou can use the\n[`KEYS`](https://redis.io/docs/latest/commands/keys) command from\n[`redis-cli`](https://redis.io/docs/latest/develop/tools/cli) or\n[Redis Insight](https://redis.io/docs/latest/develop/tools/insight) to check\nthat the \"testkey\" string key was added to the Redis database."
    }
  ],
  "examples": [
    {
      "id": "create-a-simple-app-ex0",
      "language": "bash",
      "code": "cc main.c -L/usr/local/lib -lhiredis -levent",
      "section_id": "create-a-simple-app"
    },
    {
      "id": "create-a-simple-app-ex1",
      "language": "c",
      "code": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <signal.h>\n\n#include <hiredis/hiredis.h>\n#include <hiredis/async.h>\n#include <hiredis/adapters/libevent.h>\n\n// Callback for the `GET` command. \nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    char *key = privdata;\n\n    if (reply == NULL) {\n        if (c->errstr) {\n            printf(\"errstr: %s\\n\", c->errstr);\n        }\n        return;\n    }\n\n    printf(\"Key: %s, value: %s\\n\", key, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\n// Callback to respond to successful or unsuccessful connection.\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Connected...\\n\");\n}\n\n// Callback to respond to intentional or unexpected disconnection.\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Disconnected...\\n\");\n}\n\n\nint main (int argc, char **argv) {\n#ifndef _WIN32\n    signal(SIGPIPE, SIG_IGN);\n#endif\n\n    // Create the libevent `event_base` object to track all\n    // events.\n    struct event_base *base = event_base_new();\n\n    redisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\n\n    if (c->err) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n\n    // Use the Redis libevent adapter to attach the Redis connection\n    // to the libevent main loop.\n    redisLibeventAttach(c,base);\n    \n    redisAsyncSetConnectCallback(c, connectCallback);\n    redisAsyncSetDisconnectCallback(c, disconnectCallback);\n    \n    char *key = \"testkey\";\n    char *value = \"testvalue\";\n\n    redisAsyncCommand(c, NULL, NULL, \"SET %s %s\", key, value);\n    redisAsyncCommand(c, getCallback, key, \"GET %s\", key);\n    \n    // Run the event loop.\n    event_base_dispatch(base);\n\n    return 0;\n}",
      "section_id": "create-a-simple-app"
    },
    {
      "id": "run-the-code-ex0",
      "language": "plaintext",
      "code": "Connected...\nKey: testkey, value: testvalue\nDisconnected...",
      "section_id": "run-the-code"
    }
  ]
}
