Skip to main content
Durable streams for real-time data subscriptions.
modules::stream::StreamModule

Architecture

Data Flow

When a worker triggers stream::set, the engine:
  1. Persists the data via the configured adapter (Redis or KvStore)
  2. Publishes a notification to all WebSocket clients subscribed to that stream and group
  3. Evaluates registered stream triggers and fires matching handlers
A single stream::set handles persistence, real-time delivery, and reactive logic in one operation.

Groups

Streams organize data hierarchically: stream_name > group_id > item_id.
  • stream_name identifies the top-level stream (e.g. chat, presence, dashboard)
  • group_id partitions data within a stream (e.g. room-1, team-alpha)
  • item_id uniquely identifies a record within a group (e.g. user-123, msg-456)
Clients subscribe at the group level by connecting to ws://host:port/stream/{stream_name}/{group_id}/. They receive all item-level changes within that group.

Sample Configuration

- class: modules::stream::StreamModule
  config:
    port: ${STREAM_PORT:3112}
    host: 0.0.0.0
    adapter:
      class: modules::stream::adapters::RedisAdapter
      config:
        redis_url: ${REDIS_URL:redis://localhost:6379}

Configuration

port
number
The port to listen on. Defaults to 3112.
host
string
The host to listen on. Defaults to 0.0.0.0.
auth_function
string
The authentication function to use. It’s a path to a function that will be used to authenticate the client. You can register the function using the iii SDK and then use the path to the function here.
adapter
Adapter
The adapter to use. It’s the adapter that will be used to store the streams. You can register the adapter using the iii SDK and then use the path to the adapter here.

Adapters

modules::stream::adapters::RedisAdapter

Uses Redis as the backend for the streams. Stores stream data in Redis and leverages Redis Pub/Sub for real-time event delivery.
class: modules::stream::adapters::RedisAdapter
config:
  redis_url: ${REDIS_URL:redis://localhost:6379}

Configuration

redis_url
string
The URL of the Redis instance to use.

modules::stream::adapters::KvStore

Built-in key-value store. Supports in-memory or file-based persistence. No external dependencies required.
class: modules::stream::adapters::KvStore
config:
  store_method: file_based
  file_path: ./data/streams_store.db

Configuration

store_method
string
Storage method. Options: in_memory (lost on restart) or file_based (persisted to disk).
file_path
string
Directory path for file-based storage. Each stream is stored as a separate file.

Functions

stream::set
function
Sets a value in the stream.
stream_name
string
required
The ID of the stream to set the value in.
group_id
string
required
The group ID of the stream to set the value in.
item_id
string
required
The item ID of the stream to set the value in.
data
any
required
The value to set in the stream.
old_value
any
The previous value, or null if the item was newly created.
new_value
any
required
The value now stored in the stream.
stream::get
function
Gets a value from the stream.
stream_name
string
required
The ID of the stream to retrieve the value from.
group_id
string
required
The group ID in the stream to retrieve the value from.
item_id
string
required
The item ID in the stream to retrieve.
value
any
required
The value retrieved from the stream.
stream::delete
function
Deletes a value from the stream.
stream_name
string
required
The ID of the stream to delete the value from.
group_id
string
required
The group ID in the stream to delete the value from.
item_id
string
required
The item ID in the stream to delete.
old_value
any
The value that was deleted, or null if the item did not exist.
stream::list
function
Retrieves a group from the stream. This function will return all the items in the group.
stream_name
string
required
The ID of the stream to retrieve the group from.
group_id
string
required
The group ID in the stream to retrieve the group from.
group
any[]
required
The group retrieved from the stream. It’s an array of items in the group.
stream::list_groups
function
List all groups in a stream.
stream_name
string
required
The ID of the stream to list groups from.
groups
string[]
required
An array of group IDs in the stream.
stream::list_all
function
List all streams with their group metadata.
This function takes no parameters.
stream
object[]
required
An array of stream metadata objects. Each object has an id (string) and a groups (string[]) field.
count
number
required
The total number of streams.
stream::send
function
Send a custom event to all subscribers of a stream group.
stream_name
string
required
The ID of the stream to send the event to.
group_id
string
required
The group ID in the stream to send the event to.
type
string
required
The event type string delivered to subscribers.
id
string
Optional item ID to associate the event with.
data
any
required
The event payload delivered to subscribers.
result
null
Returns null on success.
stream::update
function
Atomically update an item in the stream using a list of operations.
stream_name
string
required
The ID of the stream containing the item to update.
group_id
string
required
The group ID in the stream containing the item to update.
item_id
string
required
The item ID in the stream to update.
ops
UpdateOp[]
required
The list of atomic operations to apply. Each operation is a tagged object with a type field (set, merge, increment, decrement, or remove) and associated fields (path, value, by).
old_value
any
The previous value, or null if the item was newly created.
new_value
any
required
The value now stored in the stream after applying the operations.

Authentication

It’s possible to implement a function to handle authentication.
  1. Define a function to handle the authentication. It received one single argument with the request data.
iii.registerFunction({ id: 'onAuth' }, (input) => ({
  context: { name: 'John Doe' },
}))
  1. Make sure you add the function to the configuration file.
- class: modules::stream::StreamModule
  config:
    auth_function: onAuth
  1. Now whenever someone opens a websocket connection, the function onAuth will be called with the request data.

Trigger Types

This module adds three trigger types: stream (item changes), stream:join (WebSocket connect), and stream:leave (WebSocket disconnect).

stream:join and stream:leave

Fire when a client connects or disconnects via WebSocket. Both trigger types deliver the same payload to the handler:
subscription_id
string
required
The subscription ID, used for uniqueness and logging.
stream_name
string
required
The stream name of the subscription.
group_id
string
required
The group ID of the subscription.
id
string
The item ID of the subscription, if provided by the client.
context
object
The context generated by the authentication layer.

stream

Fires when an item changes in the stream (via stream::set, stream::update, or stream::delete). Register with a config object to filter which stream, group, or item triggers the handler:
stream_name
string
required
The stream name to watch. Only changes on this stream fire the handler.
group_id
string
If set, only changes within this group fire the handler.
item_id
string
If set, only changes to this specific item fire the handler.
condition_function_id
string
Function ID for conditional execution. The engine invokes it with the event payload; if it returns false, the handler function is not called.

Sample Code

const fn = iii.registerFunction({ id: 'onJoin' }, (input) => {
  console.log('Joined stream', input)
  return {}
})

iii.registerTrigger({
  type: 'stream:join',
  function_id: fn.id,
  config: {},
})

Usage Example: Real-Time Presence

Streams organize data by stream_name, group_id, and item_id. Use for live presence, collaborative docs, or dashboards:
import { registerWorker, TriggerAction } from 'iii-sdk'

const iii = registerWorker('ws://localhost:49134')

iii.trigger({
  function_id: 'stream::set',
  payload: {
    stream_name: 'presence',
    group_id: 'room-1',
    item_id: 'user-123',
    data: { name: 'Alice', online: true, lastSeen: new Date().toISOString() },
  },
  action: TriggerAction.Void(),
})

const user = await iii.trigger({
  function_id: 'stream::get',
  payload: {
    stream_name: 'presence',
    group_id: 'room-1',
    item_id: 'user-123',
  },
})

const roomMembers = await iii.trigger({
  function_id: 'stream::list',
  payload: {
    stream_name: 'presence',
    group_id: 'room-1',
  },
})

iii.trigger({
  function_id: 'stream::delete',
  payload: {
    stream_name: 'presence',
    group_id: 'room-1',
    item_id: 'user-123',
  },
  action: TriggerAction.Void(),
})
Clients connect via WebSocket to ws://host:3112/stream/presence/room-1/ and receive real-time updates when items change.

Usage Example: Join with Auth Context

Configure the stream module with an auth function:
- class: modules::stream::StreamModule
  config:
    port: 3112
    host: 0.0.0.0
    auth_function: stream::auth
    adapter:
      class: modules::stream::adapters::KvStore
      config:
        store_method: file_based
        file_path: ./data/stream_store
Register the auth function. Clients may send the token via Authorization: Bearer <token> (Node.js) or Sec-WebSocket-Protocol: Authorization,<token> (browser stream-client):
iii.registerFunction({ id: 'stream::auth' }, (input) => {
  const auth = input.headers?.['authorization']?.replace(/^Bearer\s+/i, '')
  const proto = input.headers?.['sec-websocket-protocol']
  const token = auth ?? (proto?.startsWith('Authorization,') ? proto.slice(13) : null)
  return token ? { context: { userId: 'user-from-token' } } : { context: null }
})
Join/leave triggers receive the auth context:
const fn = iii.registerFunction({ id: 'onJoin' }, (input) => {
  const { stream_name, group_id, id: itemId, context } = input
  if (context?.userId) {
    console.log(`User ${context.userId} joined ${stream_name}/${group_id}/${itemId}`)
  }
  return {}
})

iii.registerTrigger({
  type: 'stream:join',
  function_id: fn.id,
  config: {},
})

Usage Example: Conditional Join

const conditionFn = iii.registerFunction(
  { id: 'conditions::requireContext' },
  async (input) => input.context?.userId != null,
)

const fn = iii.registerFunction({ id: 'onJoin' }, (input) => {
  console.log('User joined:', input.context?.userId, input.stream_name)
  return {}
})

iii.registerTrigger({
  type: 'stream:join',
  function_id: fn.id,
  config: { condition_function_id: conditionFn.id },
})