StateAdapter interface defines the contract for managing persistent state in Chat SDK. State adapters handle thread subscriptions, distributed locking, and caching.
Built-in Implementations
Chat SDK provides three state adapter implementations:- @chat-adapter/state-redis - Production Redis adapter
- @chat-adapter/state-ioredis - ioredis-based adapter with cluster support
- @chat-adapter/state-memory - In-memory adapter for development
Interface
Methods
connect()
Establish connection to the state backend.disconnect()
Close connection to the state backend.subscribe()
Subscribe to a thread to receive all future messages.string
required
The thread ID to subscribe to
unsubscribe()
Unsubscribe from a thread.string
required
The thread ID to unsubscribe from
isSubscribed()
Check if currently subscribed to a thread.string
required
The thread ID to check
boolean
Whether the thread is subscribed
acquireLock()
Acquire a distributed lock on a thread with a TTL.string
required
The thread ID to lock
number
required
Time-to-live in milliseconds
Lock | null
Lock object if acquired, null if already locked
releaseLock()
Release a previously acquired lock.Lock
required
The lock object to release
extendLock()
Extend the TTL of an existing lock.Lock
required
The lock object to extend
number
required
New time-to-live in milliseconds
boolean
Whether the lock was successfully extended
get()
Retrieve a cached value by key.string
required
Cache key
T | null
Cached value or null if not found
set()
Store a value in cache with optional TTL.string
required
Cache key
T
required
Value to cache
number
Optional time-to-live in milliseconds
delete()
Delete a cached value by key.string
required
Cache key to delete