iii-channels
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseChannels
通道
Comparable to: Unix pipes, gRPC streaming, WebSocket data streams
类似技术:Unix管道、gRPC流式传输、WebSocket数据流
Key Concepts
核心概念
Use the concepts below when they fit the task. Not every worker needs channels.
- A Channel is a WebSocket-backed binary stream between two endpoints (writer and reader)
- returns a writer/reader pair plus serializable refs that can be passed to other workers
createChannel() - StreamChannelRef is a serializable reference (channel_id, access_key, direction) that can be included in function payloads
- Writers send binary data (chunked into 64KB frames) and text messages
- Readers consume binary chunks via or receive text messages via callbacks
readAll() - Consumers must construct a reader from a serializable (e.g.,
StreamChannelRef) rather than using the producer-side reader object returned byChannelReader::new(...)createChannel() - Channels work cross-worker and cross-language — a Python writer can stream to a Rust reader
当任务匹配时使用以下概念,并非所有Worker都需要通道。
- Channel 是基于WebSocket的、连接两个端点(writer和reader)的二进制流
- 返回writer/reader对,以及可序列化的引用,这些引用可传递给其他Worker
createChannel() - StreamChannelRef 是可序列化的引用(包含channel_id、access_key、direction),可包含在函数负载中
- Writer发送二进制数据(分割为64KB帧)和文本消息
- Reader通过消费二进制块,或通过回调接收文本消息
readAll() - 消费者必须从可序列化的构造reader(例如
StreamChannelRef),而非使用ChannelReader::new(...)返回的生产者端reader对象createChannel() - 通道支持跨Worker、跨语言使用——Python writer可向Rust reader流式传输数据
Architecture
架构
A function creates a channel via , receiving a writer and reader pair. The writer ref or reader ref is passed to another function (potentially in a different worker/language) via a trigger payload. The engine brokers the WebSocket connection between the two endpoints. Binary data flows directly between workers through the engine's channel endpoint.
createChannel()函数通过创建通道,获取writer和reader对。writer引用或reader引用通过触发负载传递给另一个函数(可能位于不同的Worker或语言环境中)。引擎负责代理两个端点之间的WebSocket连接。二进制数据通过引擎的通道端点在Worker之间直接传输。
createChannel()iii Primitives Used
使用的iii原语
| Primitive | Purpose |
|---|---|
| Create a channel, returns writer + reader pair |
| Send binary data (chunked into 64KB frames) |
| Send a text message through the channel |
| Close the writer end |
| Read entire stream into a single buffer |
| Register callback for text messages |
| Serializable reference to pass between workers |
| 原语 | 用途 |
|---|---|
| 创建通道,返回writer + reader对 |
| 发送二进制数据(分割为64KB帧) |
| 通过通道发送文本消息 |
| 关闭writer端 |
| 将整个流读取到单个缓冲区中 |
| 注册文本消息的回调函数 |
| 可序列化的引用,用于在Worker间传递 |
Reference Implementation
参考实现
- TypeScript: ../references/channels.js
- Python: ../references/channels.py
- Rust: ../references/channels.rs
Each reference shows the same patterns (channel creation, binary streaming, text messages, cross-function handoff) in its respective language.
- TypeScript: ../references/channels.js
- Python: ../references/channels.py
- Rust: ../references/channels.rs
每个示例都展示了相同的模式(通道创建、二进制流传输、文本消息、跨函数传递),对应各自的语言。
Common Patterns
常见模式
Code using this pattern commonly includes, when relevant:
- — create a channel pair (producer access)
const channel = await iii.createChannel() - /
channel.writer.stream.write(buffer)— send binary datachannel.writer.write(data) - — send text metadata
channel.writer.sendMessage(JSON.stringify({ type: 'metadata', ... })) - — signal end of stream
channel.writer.close() - Pass or
channel.readerRefin trigger payloads for cross-worker streamingchannel.writerRef - Consumer must reconstruct the reader from the ref: e.g.,
new ChannelReader(iii.address, readerRef) - — read entire stream (consumer behavior)
const data = await reader.readAll() - — handle text messages (consumer behavior)
reader.onMessage(msg => { ... })
使用此模式的代码通常包含以下相关内容:
- — 创建通道对(生产者权限)
const channel = await iii.createChannel() - /
channel.writer.stream.write(buffer)— 发送二进制数据channel.writer.write(data) - — 发送文本元数据
channel.writer.sendMessage(JSON.stringify({ type: 'metadata', ... })) - — 标记流结束
channel.writer.close() - 在触发负载中传递或
channel.readerRef以实现跨Worker流式传输channel.writerRef - 消费者必须从引用重建reader:例如
new ChannelReader(iii.address, readerRef) - — 读取整个流(消费者行为)
const data = await reader.readAll() - — 处理文本消息(消费者行为)
reader.onMessage(msg => { ... })
Adapting This Pattern
模式适配
Use the adaptations below when they apply to the task.
- Use channels for large data transfers that shouldn't be serialized into JSON payloads
- Pass to a processing function and
readerRefto a producing function for pipeline patternswriterRef - Use text messages for metadata/signaling alongside binary data streams
- Set when the reader may be slower than the writer to apply backpressure
bufferSize - Channels work cross-language — a TypeScript producer can stream to a Rust consumer
当任务符合以下情况时使用相应的适配方式:
- 对于不应序列化为JSON负载的大型数据传输,使用通道
- 为管道模式,将传递给处理函数,将
readerRef传递给生产函数writerRef - 结合二进制数据流,使用文本消息传递元数据/信号
- 当reader速度慢于writer时,设置以实现背压
bufferSize - 通道支持跨语言使用——TypeScript生产者可向Rust消费者流式传输数据
Pattern Boundaries
模式边界
- For key-value state persistence, prefer .
iii-state-management - For stream CRUD (named streams with groups/keys), prefer .
iii-realtime-streams - For pub/sub messaging, prefer triggers with type.
subscribe - Stay with when the primary problem is binary data streaming between workers.
iii-channels
- 对于键值状态持久化,优先使用。
iii-state-management - 对于流CRUD(带分组/键的命名流),优先使用。
iii-realtime-streams - 对于发布/订阅消息,优先使用类型的触发器。
subscribe - 当核心需求是Worker间二进制流传输时,使用。
iii-channels
When to Use
使用场景
- Use this skill when the task is primarily about in the iii engine.
iii-channels - Triggers when the request directly asks for this pattern or an equivalent implementation.
- 当任务主要涉及iii引擎中的时,使用此技能。
iii-channels - 当请求直接要求此模式或等效实现时触发。
Boundaries
边界限制
- Never use this skill as a generic fallback for unrelated tasks.
- You must not apply this skill when a more specific iii skill is a better fit.
- Always verify environment and safety constraints before applying examples from this skill.
- 切勿将此技能作为无关任务的通用 fallback。
- 当更特定的iii技能更合适时,不得应用此技能。
- 在应用此技能的示例之前,务必验证环境和安全约束。