Skip to main content
The 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:

Interface

Methods

connect()

Establish connection to the state backend.
Called automatically by Chat SDK during initialization.

disconnect()

Close connection to the state backend.
Called automatically by Chat SDK during shutdown.

subscribe()

Subscribe to a thread to receive all future messages.
string
required
The thread ID to subscribe to
Subscriptions persist across restarts and are stored indefinitely until explicitly unsubscribed.

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
Locks prevent concurrent processing of messages in the same thread across multiple instances.

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

Implementing a Custom Adapter

To create a custom state adapter:

See Also