Skip to main content

Overview

Channels provide bidirectional streaming for transferring large datasets between workers without loading everything into memory. They support both binary data streaming and text message passing, making them ideal for processing files, media, logs, and real-time data feeds.

Channel Architecture

Channels are created by the Engine and consist of:
1

Writer Endpoint

Sends data to the channel. Multiple chunks can be written sequentially.
2

Engine Buffer

Stores data temporarily (configurable buffer size, default 64 items)
3

Reader Endpoint

Receives data from the channel. Supports Node.js Readable stream interface.

Creating Channels

Basic Channel Creation

Channel structure:
StreamChannelRef structure:

Custom Buffer Size

Buffer size determines how many chunks can be queued before writers block. Larger buffers improve throughput but use more memory.
Implementation:

ChannelWriter

Writing Binary Data

ChannelWriter provides a Node.js Writable stream interface:

Automatic Chunking

Large buffers are automatically split into 64KB frames:
The 64KB frame size (ChannelWriter.FRAME_SIZE) balances WebSocket message overhead with memory efficiency.

Piping Streams

Pipe Node.js readable streams directly:

Sending Text Messages

Send out-of-band text messages (separate from binary stream):
Implementation:
Text messages and binary chunks use the same WebSocket but are distinguished by message type. Text messages don’t affect the binary stream.

Closing Channels

ChannelWriter class structure:
Location: packages/node/iii/src/channels.ts:5-111

ChannelReader

Reading Binary Data

ChannelReader provides a Node.js Readable stream interface:

Collecting Full Stream

Piping to Destination

Receiving Text Messages

Listen for out-of-band text messages:
ChannelReader class structure:
Location: packages/node/iii/src/channels.ts:113-174

Channel References in Function Calls

Channel references are serializable and can be passed as function arguments:
Automatic resolution: The Engine automatically resolves channel references to ChannelReader/ChannelWriter instances:
Channel references are resolved recursively in objects and arrays. Nested channel refs work automatically.

Real-World Examples

Example 1: Data Processing Pipeline

Example 2: Bidirectional Streaming with Progress

Example 3: File Upload/Download

Channel URL Format

Channels connect via dedicated WebSocket endpoints:
Example URL:

Error Handling

Writer Errors

Reader Errors

Connection Errors

Channel WebSockets do not automatically reconnect. If the connection drops, the stream will emit an error and close.

Performance Considerations

Frame Size

The 64KB frame size balances:
  • Smaller frames: Lower latency, higher overhead
  • Larger frames: Higher throughput, more memory

Backpressure Handling

Node.js streams automatically handle backpressure:

Buffer Size Tuning

Adjust channel buffer size based on use case:

Next Steps

Streaming

Learn about the Stream API for real-time data

Functions

Pass channels as function arguments