Advanced Features
State & Stream Triggers
Build reactive workflows that respond to state changes and stream events automatically
State and stream triggers enable reactive patterns where Steps execute automatically in response to data changes — no polling, no manual coordination required.
State Triggers
State triggers fire when data in Motia's state store changes. The condition function determines which changes should activate the Step.
Parallel Merge Pattern
A common use case is triggering a Step when all parallel tasks complete:
StateTriggerInput Type
The handler receives a StateTriggerInput object:
| Field | Description |
|---|---|
group_id | The state namespace (e.g., 'tasks', 'orders') |
item_id | The key of the changed item |
new_value | The value after the change (null on delete) |
old_value | The value before the change (null on create) |
State Trigger Use Cases
- Parallel merge — Wait for all parallel tasks to complete before proceeding
- Threshold alerts — Trigger when a counter reaches a threshold
- Status transitions — React to status field changes (e.g.,
pending->approved) - Data validation — Validate data after it's written and trigger corrections
Stream Triggers
Stream triggers fire when stream items are created, updated, deleted, or when custom events are sent. They enable Steps to react to real-time data changes.
Basic Stream Trigger
StreamWrapperMessage Type
The event field contains one of:
| Event Type | Description | Data |
|---|---|---|
create | A new item was created | { type: 'create', data: TStreamData } |
update | An existing item was updated | { type: 'update', data: TStreamData } |
delete | An item was deleted | { type: 'delete', data: TStreamData } |
event | A custom event was sent | { type: 'event', data: { type: string, data: TEventData } } |
Stream Trigger Configuration
| Property | Description |
|---|---|
streamName | Name of the stream to watch (required) |
groupId | Filter to a specific group (optional) |
itemId | Filter to a specific item (optional) |
condition | Function to filter events (optional) |
Stream Trigger Use Cases
- Notifications — Send notifications when stream data changes
- Audit logging — Log all changes to a stream for compliance
- Cascade updates — Update related data when a stream item changes
- Real-time analytics — Process stream events for dashboards and metrics