|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Use `hiredis` in conjunction with the `libevent` framework. |
| 13 | +linkTitle: libevent integration |
| 14 | +title: Integrate hiredis with a libevent app |
| 15 | +weight: 60 |
| 16 | +--- |
| 17 | + |
| 18 | +The [`libevent`](https://libevent.org/) library provides an |
| 19 | +implementation of an event loop that lets you call functions |
| 20 | +asynchronously in response to events. This guide explains |
| 21 | +how to use `hiredis` to connect to a Redis server from a |
| 22 | +`libevent` app. |
| 23 | + |
| 24 | +## Install `libevent` |
| 25 | + |
| 26 | +The [`libevent` home page](https://libevent.org/) has links to download |
| 27 | +all versions of the library, but you should use the latest version |
| 28 | +unless there is a specific version you need to target. |
| 29 | + |
| 30 | +When you have downloaded `libevent`, follow the instructions in the |
| 31 | +`README` file to compile and install the library. |
| 32 | + |
| 33 | +## Create a simple app |
| 34 | + |
| 35 | +For a real project, you would build your code with a makefile, but for |
| 36 | +this simple test, you can just place it in a file called `main.c` and |
| 37 | +build it with the following command (assuming you used `make install` to |
| 38 | +install the `libhiredis` and `libevent` libraries): |
| 39 | + |
| 40 | +```bash |
| 41 | +cc main.c -L/usr/local/lib -lhiredis -levent |
| 42 | +``` |
| 43 | + |
| 44 | +See [Build and install]({{< relref "/develop/clients/hiredis#build-and-install" >}}) |
| 45 | +to learn how to build `hiredis`, if you have not already done so. |
| 46 | + |
| 47 | +Now, add the following code in `main.c`. An explanation follows the |
| 48 | +code example: |
| 49 | + |
| 50 | +```c |
| 51 | +#include <stdio.h> |
| 52 | +#include <stdlib.h> |
| 53 | +#include <string.h> |
| 54 | +#include <signal.h> |
| 55 | + |
| 56 | +#include <hiredis/hiredis.h> |
| 57 | +#include <hiredis/async.h> |
| 58 | +#include <hiredis/adapters/libevent.h> |
| 59 | + |
| 60 | +// Callback for the `GET` command. |
| 61 | +void getCallback(redisAsyncContext *c, void *r, void *privdata) { |
| 62 | + redisReply *reply = r; |
| 63 | + char *key = privdata; |
| 64 | + |
| 65 | + if (reply == NULL) { |
| 66 | + if (c->errstr) { |
| 67 | + printf("errstr: %s\n", c->errstr); |
| 68 | + } |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + printf("Key: %s, value: %s\n", key, reply->str); |
| 73 | + |
| 74 | + /* Disconnect after receiving the reply to GET */ |
| 75 | + redisAsyncDisconnect(c); |
| 76 | +} |
| 77 | + |
| 78 | +// Callback to respond to successful or unsuccessful connection. |
| 79 | +void connectCallback(const redisAsyncContext *c, int status) { |
| 80 | + if (status != REDIS_OK) { |
| 81 | + printf("Error: %s\n", c->errstr); |
| 82 | + return; |
| 83 | + } |
| 84 | + printf("Connected...\n"); |
| 85 | +} |
| 86 | + |
| 87 | +// Callback to respond to intentional or unexpected disconnection. |
| 88 | +void disconnectCallback(const redisAsyncContext *c, int status) { |
| 89 | + if (status != REDIS_OK) { |
| 90 | + printf("Error: %s\n", c->errstr); |
| 91 | + return; |
| 92 | + } |
| 93 | + printf("Disconnected...\n"); |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +int main (int argc, char **argv) { |
| 98 | +#ifndef _WIN32 |
| 99 | + signal(SIGPIPE, SIG_IGN); |
| 100 | +#endif |
| 101 | + |
| 102 | + // Create the libevent `event_base` object to track all |
| 103 | + // events. |
| 104 | + struct event_base *base = event_base_new(); |
| 105 | + |
| 106 | + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); |
| 107 | + |
| 108 | + if (c->err) { |
| 109 | + printf("Error: %s\n", c->errstr); |
| 110 | + return 1; |
| 111 | + } |
| 112 | + |
| 113 | + // Use the Redis libevent adapter to attach the Redis connection |
| 114 | + // to the libevent main loop. |
| 115 | + redisLibeventAttach(c,base); |
| 116 | + |
| 117 | + redisAsyncSetConnectCallback(c, connectCallback); |
| 118 | + redisAsyncSetDisconnectCallback(c, disconnectCallback); |
| 119 | + |
| 120 | + char *key = "testkey"; |
| 121 | + char *value = "testvalue"; |
| 122 | + |
| 123 | + redisAsyncCommand(c, NULL, NULL, "SET %s %s", key, value); |
| 124 | + redisAsyncCommand(c, getCallback, key, "GET %s", key); |
| 125 | + |
| 126 | + // Run the event loop. |
| 127 | + event_base_dispatch(base); |
| 128 | + |
| 129 | + return 0; |
| 130 | +} |
| 131 | +``` |
| 132 | +
|
| 133 | +The code calls |
| 134 | +[`event_base_new()`](https://libevent.org/doc/event_8h.html#af34c025430d445427a2a5661082405c3) |
| 135 | +to initialize the core |
| 136 | +[`event_base`](https://libevent.org/doc/structevent__base.html) |
| 137 | +object that manages the event loop. It then creates a standard |
| 138 | +[asynchronous connection]({{< relref "/develop/clients/hiredis/connect#asynchronous-connection" >}}) |
| 139 | +to Redis and uses the `libevent` adapter function `redisLibeventAttach()` to |
| 140 | +attach the connection to the event loop. |
| 141 | +
|
| 142 | +After setting the [connection callbacks]({{< relref "/develop/clients/hiredis/connect#asynchronous-connection" >}}), the code issues two asynchronous |
| 143 | +Redis commands (see |
| 144 | +[Construct asynchronous commands]({{< relref "/develop/clients/hiredis/issue-commands#construct-asynchronous-commands" >}}) |
| 145 | +for more information). |
| 146 | +The final step is to call |
| 147 | +[`event_base_dispatch()`](https://libevent.org/doc/event_8h.html#a19d60cb72a1af398247f40e92cf07056) |
| 148 | +to start the event loop. This will wait for the commands to be processed and |
| 149 | +then exit when the Redis connection is closed in the `getCallback()` function. |
| 150 | +
|
| 151 | +## Run the code |
| 152 | +
|
| 153 | +If you compile and run the code, you will see the following output, |
| 154 | +showing that the callbacks executed correctly: |
| 155 | +
|
| 156 | +``` |
| 157 | +Connected... |
| 158 | +Key: testkey, value: testvalue |
| 159 | +Disconnected... |
| 160 | +``` |
| 161 | +
|
| 162 | +You can use the |
| 163 | +[`KEYS`]({{< relref "/commands/keys" >}}) command from |
| 164 | +[`redis-cli`]({{< relref "/develop/tools/cli" >}}) or |
| 165 | +[Redis Insight]({{< relref "/develop/tools/insight" >}}) to check |
| 166 | +that the "testkey" string key was added to the Redis database. |
0 commit comments