Skip to main content
Distributed key-value state storage with scope-based organization and reactive triggers that fire on any state change.

Architecture

State is server-side key-value storage with trigger-based reactivity. Unlike streams, state does not push updates to WebSocket clients — it fires triggers that workers handle server-side.

Sample Configuration

Configuration

Adapter
The adapter to use for state persistence and distribution. Defaults to modules::state::adapters::KvStore when not specified.

Adapters

modules::state::adapters::KvStore

Built-in key-value store. Supports both in-memory and file-based persistence.

Configuration

string
Storage method. Options: in_memory (lost on restart) or file_based (persisted to disk).
string
Directory path for file-based storage. Each scope is stored as a separate file.
number
Interval in milliseconds between automatic disk saves. Defaults to 5000.

modules::state::adapters::RedisAdapter

Uses Redis as the state backend.

Configuration

string
The URL of the Redis instance to use.

modules::state::adapters::Bridge

Forwards state operations to a remote III Engine instance via the Bridge Client.

Functions

function
Set a value in state. Fires a state:created trigger if the key did not exist, or state:updated if it did.
string
required
The scope (namespace) to organize state within.
string
required
The key to store the value under.
any
required
The value to store. Can be any JSON-serializable value. Also accepted as data (backward-compatible alias).
any
The previous value, or null if the key did not exist.
any
The value that was stored.
function
Get a value from state.
string
required
The scope to read from.
string
required
The key to retrieve.
any
The stored value, or null if the key does not exist.
function
Delete a value from state. Fires a state:deleted trigger.
string
required
The scope to delete from.
string
required
The key to delete.
any
The deleted value, or null if the key did not exist.
function
Atomically update a value using one or more operations. Fires state:created or state:updated depending on whether the key existed.
string
required
The scope to update within.
string
required
The key to update.
UpdateOp[]
required
Array of update operations applied in order. Each operation is an object with a single key:
any
The value before the operations were applied, or null if the key did not exist.
any
The value after all operations were applied.
function
List all values within a scope.
string
required
The scope to list entries from.
A flat JSON array of all stored values within the scope: any[].
function
List all scopes that contain state data.
An object with a single groups field:
string[]
A sorted, deduplicated array of all scope names that contain at least one key.

Trigger Type

This module adds a new Trigger Type: state. When a state value is created, updated, or deleted, all registered state triggers are evaluated and fired if they match.

State Event Payload

When the trigger fires, the handler receives a state event object:
string
Always "state".
string
The kind of change: "state:created", "state:updated", or "state:deleted".
string
The scope where the change occurred.
string
The key that changed.
any
The previous value before the change, or null for newly created keys.
any
The new value after the change. null for deleted keys.

Sample Code

Usage Example: User Profile with Reactive Sync

Store user profiles in state and react when they change:

Usage Example: Conditional Trigger

Only process profile updates when the email field changed:

State Flow