How to Stream Binary Payloads with VireoRPC
Streaming binary data across a WebAssembly host-guest boundary introduces unique memory management constraints. Unlike standard TCP sockets, WebAssembly modules communicate through linear memory slices, which means framing boundaries must be explicit so neither side reads past an allocated buffer. VireoRPC solves this by wrapping every binary chunk in a compact length-prefixed varint frame.
Before writing code, it helps to understand how VireoRPC handles payload sizing and backpressure. Each frame begins with an unsigned LEB128 varint specifying the payload byte count, followed by a flags byte. Keeping payloads at or below 64 KB lets the encoder stay inside fast linear memory slabs, yielding a p99 encode latency of 0.42 ms on 64 KB payloads. Whenever an application tries to push a frame larger than the configured cap, the encoder stops immediately and emits ERR_FRAME_CAP_104 so your caller can split the payload into continuation frames.
Step-by-Step Implementation
Initialize a VireoRPC channel with the default 64 KB frame cap and wrap your write call in a handler for ERR_FRAME_CAP_104:
import { VireoChannel, VireoError } from "vireorpc-wasm";
const ch = new VireoChannel({ framing: "varint", maxFrameBytes: 65536 });
try {
await ch.sendBinary(payloadBytes);
} catch (err) {
if (err instanceof VireoError && err.code === "ERR_FRAME_CAP_104") {
await ch.sendChunked(payloadBytes, 65536);
}
}
Once chunking is in place, verify tail latency under load using the bundled CLI profiler to confirm p99 stays near 0.42 ms.