Looking for the chatbot template? It's now here.

ioredis

Alternative Redis state adapter using ioredis with Cluster and Sentinel support.

An alternative Redis state adapter using ioredis. Use this if you already have ioredis in your project or need Redis Cluster/Sentinel support.

Installation

Terminal
pnpm add @chat-adapter/state-ioredis

Usage

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!,
  }),
});

Using an existing client

lib/bot.ts
import Redis from "ioredis";

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

const state = createIORedisState({ client });

Configuration

OptionRequiredDescription
urlYes*Redis connection URL
clientNoExisting ioredis client instance
keyPrefixNoPrefix for all keys (default: "chat-sdk")

*Either url or client is required.

When to use ioredis vs redis

Use @chat-adapter/state-ioredis when:

  • You already use ioredis in your project
  • You need Redis Cluster support
  • You need Redis Sentinel support
  • You prefer the ioredis API

Use @chat-adapter/state-redis when:

  • You want the official Redis client
  • You're starting a new project
  • You don't need Cluster or Sentinel

Key structure

{keyPrefix}:subscriptions     - SET of subscribed thread IDs
{keyPrefix}:lock:{threadId}   - Lock key with TTL

Features

  • Persistent subscriptions across restarts
  • Distributed locking across multiple instances
  • Automatic reconnection
  • Redis Cluster support
  • Redis Sentinel support
  • Key prefix namespacing