Memory

In-memory state adapter for local development and tests. State is lost on restart and locks don't work across instances, so don't use it in production.

Create a new bot

Scaffold a Next.js bot that uses the Memory state adapter with create-chat-sdk. Replace slack with your platform adapter:

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

To add the adapter to an existing app, start at Install.

Install

pnpm add @chat-adapter/state-memory

Quick start

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

const bot = new Chat({
  userName: "mybot",
  adapters: { /* ... */ },
  state: createMemoryState(),
});

createMemoryState() takes no arguments. Subscriptions, locks, cache entries, lists, and queues live in the current process and disappear when the process exits.

Use it for local development and for tests that need a real StateAdapter without a Redis or Postgres dependency.

Limitations

Everything the adapter stores lives in one process's memory. In production, that leads to these problems:

  • State doesn't survive a restart or redeploy. Thread subscriptions, thread state from setState(), and queued messages are lost, so new messages in previously subscribed threads no longer reach onSubscribedMessage.
  • State isn't shared between instances. A thread subscribed on one instance looks unsubscribed on another, and deduplication only catches repeat deliveries that reach the same instance.
  • Locks only apply within one process. Two instances can acquire the lock for the same thread and handle messages in it at the same time.

When NODE_ENV is production, connect() logs a warning that the memory adapter isn't recommended for production. For production, use @chat-adapter/state-redis, @chat-adapter/state-ioredis, or @chat-adapter/state-pg.

Feature support

Capabilities

FeatureSupported
Persistence
Multi-instance
SubscriptionsIn-memory
Distributed lockingSingle process
Key-value cachingIn-memory
Lists
Queues
Automatic reconnect
Cluster support
Sentinel support
Key prefix namespacing

More adapters

On this page