{
  "schema_version": 2,
  "id": "develop/clients/hiredis/connect",
  "title": "Connect",
  "url": "https://redis.io/docs/latest/develop/clients/hiredis/connect/",
  "summary": "Connect to the server with `hiredis`.",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-10-12T14:34:35+01:00",
  "page_type": "content",
  "content_hash": "2a7f71daf5edf5f600622ab627a4b2e1e04b3883818f98c7e8908ef62fc9b5d0",
  "sections": [
    {
      "id": "basic-synchronous-connection",
      "title": "Basic synchronous connection",
      "role": "overview",
      "text": "The example below creates a simple synchronous connection to a local\nRedis server and tests the connection, before closing it with\n`redisFree()`. The `redisConnect()` function takes just a hostname\nand port as its arguments, and returns a context object.\n\n[code example]"
    },
    {
      "id": "asynchronous-connection",
      "title": "Asynchronous connection",
      "role": "content",
      "text": "You can also connect to Redis using an asynchronous API.\nThe `redisAsyncConnect()` call that creates the context is\nsimilar to the synchronous function `redisConnect()`, but it returns the\ncontext object immediately before the connection is complete.\nIt lets you supply callbacks to respond when a connection is successful\nor to handle any errors that may occur.\n\nThe following code creates an asynchronous connection and\nsets the context callbacks. Note that you must also include the\n`async.h` header to access the asynchronous API.\n\n[code example]\n\nThe callback functions have a simple signature that receives\nthe context object and a status code. See\n[Handling errors](https://redis.io/docs/latest/develop/clients/hiredis/handle-replies#handling-errors)\nfor a list of the possible status codes.\n\n[code example]\n\nUse the `redisAsyncCommand()` function to issue Redis commands\nwith an asynchronous connection. This is similar to the equivalent\nsynchronous function `redisCommand()` but also lets you supply a callback\nand a custom data pointer to process the response to the command. See\n[Construct asynchronous commands](https://redis.io/docs/latest/develop/clients/hiredis/issue-commands#construct-asynchronous-commands) for more\ninformation.\n\nNote that you should normally disconnect asynchronously from a\ncallback when you have finished using the connection.\nUse `redisAsyncDisconnect()` to disconnect gracefully, letting\npending commands execute and activate their callbacks.\nUse `redisAsyncFree()` to disconnect immediately. If you do this then\nany pending callbacks from commands that have already executed will be\ncalled with a `NULL` reply pointer."
    },
    {
      "id": "redis-sentinel",
      "title": "Redis Sentinel",
      "role": "content",
      "text": "`hiredis` doesn't support [Redis Sentinel](https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel).\nIt has no API to find the current primary from a set of Sentinels or to reconnect after a\nfailover."
    }
  ],
  "examples": [
    {
      "id": "basic-synchronous-connection-ex0",
      "language": "c",
      "code": "#include <stdio.h>\n#include <stdlib.h>\n\n#include <hiredis/hiredis.h>\n    .\n    .\n    .\n\n// The `redisContext` type represents the connection\n// to the Redis server. Here, we connect to the\n// default host and port.\nredisContext *c = redisConnect(\"127.0.0.1\", 6379);\n\n// Check if the context is null or if a specific\n// error occurred.\nif (c == NULL || c->err) {\n    if (c != NULL) {\n        printf(\"Error: %s\\n\", c->errstr);\n        // handle error\n    } else {\n        printf(\"Can't allocate redis context\\n\");\n    }\n\n    exit(1);\n}\n\n// Set a string key.\nredisReply *reply = redisCommand(c, \"SET foo bar\");\nprintf(\"Reply: %s\\n\", reply->str); // >>> Reply: OK\nfreeReplyObject(reply);\n\n// Get the key we have just stored.\nreply = redisCommand(c, \"GET foo\");\nprintf(\"Reply: %s\\n\", reply->str); // >>> Reply: bar\nfreeReplyObject(reply);\n\n// Close the connection.\nredisFree(c);",
      "section_id": "basic-synchronous-connection"
    },
    {
      "id": "asynchronous-connection-ex0",
      "language": "c",
      "code": "#include <stdio.h>\n#include <stdlib.h>\n\n#include <hiredis/hiredis.h>\n#include <hiredis/async.h>\n    .\n    .\n    .\n\nredisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\n\nif (c->err) {\n    printf(\"Error: %s\\n\", c->errstr);\n    return 1;\n}\n\n// Set callbacks to respond to successful or unsuccessful\n// connection and disconnection.\nredisAsyncSetConnectCallback(c, connectCallback);\nredisAsyncSetDisconnectCallback(c, disconnectCallback);\n\nchar *key = \"testkey\";\nchar *value = \"testvalue\";\n\n// Status reply is ignored.\nredisAsyncCommand(c, NULL, NULL, \"SET %s %s\", key, value);\n\n// Reply handled by `getCallback()` function.\nredisAsyncCommand(c, getCallback, key, \"GET %s\", key);",
      "section_id": "asynchronous-connection"
    },
    {
      "id": "asynchronous-connection-ex1",
      "language": "c",
      "code": "void 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\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}",
      "section_id": "asynchronous-connection"
    }
  ]
}
