ioredis

Redis state adapter for Chat SDK built on ioredis. Use it if you already depend on ioredis or need Redis Sentinel.

Create a new bot

Scaffold a Next.js bot that stores state in Redis through ioredis with create-chat-sdk. Replace slack with your platform adapter:

pnpm create chat-sdk@latest my-bot --adapter slack ioredis -y

The CLI adds @chat-adapter/state-ioredis, wires createIoRedisState({ url: process.env.REDIS_URL }) into src/lib/bot.ts, and lists REDIS_URL in .env.example. To add the adapter to an existing app, start at Install.

Install

pnpm add @chat-adapter/state-ioredis

Quick start

lib/bot.ts
import { Chat } from "chat";
import { createIoRedisState } from "@chat-adapter/state-ioredis";

const bot = new Chat({
  userName: "mybot",
  adapters: { /* ... */ },
  state: createIoRedisState({ url: process.env.REDIS_URL! }),
});

To provision Upstash Redis or Redis Cloud for a Vercel project with the Vercel CLI, see Vercel Marketplace on the Redis adapter page. The same connection URL works with this adapter.

When to choose ioredis vs redis

Both adapters implement the same state operations with the same key layout. The difference is the Redis client underneath.

Use @chat-adapter/state-ioredis when:

  • You already use ioredis in your project.
  • You need Redis Sentinel. Pass a Sentinel-configured Redis client as client.
  • You prefer the ioredis API.

Use @chat-adapter/state-redis when:

  • You want the official redis client.
  • You're starting a new project and don't need Sentinel.

Neither adapter supports Redis Cluster. The ioredis adapter's client option accepts a Redis instance, and an ioredis Cluster doesn't satisfy that type.

Configuration

Prop

Type

Either url or client is required.

Environment variables

The adapter doesn't read environment variables itself, so pass the URL in your code.

VariableRequiredDescription
REDIS_URLNoRedis connection URL. The quick start reads it and passes it as url.

Using an existing client

Pass an ioredis client as client. ioredis connects on its own, and the adapter's disconnect() doesn't quit a client it didn't create.

lib/state.ts
import Redis from "ioredis";
import { createIoRedisState } from "@chat-adapter/state-ioredis";

const client = new Redis("redis://localhost:6379");

export const state = createIoRedisState({ client });

Data model

Every key starts with keyPrefix, which defaults to "chat-sdk". The layout matches @chat-adapter/state-redis.

KeyRedis typeContents
{keyPrefix}:subscriptionsSetSubscribed thread IDs
{keyPrefix}:lock:{threadId}StringLock token, with a TTL
{keyPrefix}:cache:{key}StringJSON-serialized cache value, with an optional TTL
{keyPrefix}:list:{key}ListJSON-serialized list entries, with an optional TTL
{keyPrefix}:queue:{threadId}ListQueued messages for a thread, with a TTL

Feature support

Capabilities

FeatureSupported
Persistence
Multi-instance
Subscriptions
Distributed locking
Key-value caching
Lists
Queues
Automatic reconnect
Cluster support
Sentinel support
Key prefix namespacing

More adapters

On this page