Message Passing and Channels Between WASM Threads
Coordinate parallel WASM workers using message passing patterns and channel abstractions instead of raw shared memory.
Message Passing and Channels Between WASM Threads is a free WebAssembly (WASM) for High Performance Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebAssembly (WASM) for High Performance Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Models of Concurrency
Concurrent systems coordinate either by sharing memory (SharedArrayBuffer + atomics) or by passing messages. Message passing avoids many data races by design.
postMessage Basics
Web Workers communicate with postMessage. Data is structured-cloned, so each side gets its own copy.
const worker = new Worker("worker.js");
worker.postMessage({ cmd: "start", n: 1000 });
worker.onmessage = (e) => console.log("result", e.data);Transferable Objects
To avoid copying large buffers, transfer ownership instead of cloning.
const buf = new ArrayBuffer(1024 * 1024);
worker.postMessage({ buf }, [buf]);
// buf is now neutered in the senderDesigning a Channel
A channel is a typed queue abstraction layered over postMessage: a send side and a receive side with backpressure and request IDs.
Request/Response Correlation
Attach a unique id to each message so async replies can be matched to their originating request via a pending-promise map.
const pending = new Map();
let nextId = 1;
function call(payload) {
const id = nextId++;
return new Promise((res) => {
pending.set(id, res);
worker.postMessage({ id, payload });
});
}Fan-Out Work Pools
For data parallelism, spawn N workers and round-robin tasks to them. Collect results as they arrive, preserving order with the request IDs.
Combining with Shared Memory
Message passing for control, SharedArrayBuffer for bulk data is a powerful hybrid: send a small message saying which slice to process, while the data lives in shared memory.
Avoiding Deadlocks
Pure message-passing systems still deadlock if two parties each wait for the other. Keep protocols acyclic and prefer non-blocking sends.
Serialization Cost
Structured clone is convenient but costly for big graphs. Prefer transferables or shared memory when payloads grow large.
Worker Lifecycle
Create workers up front in a pool and reuse them; spinning up a worker per task wastes time loading and instantiating the WASM module each time.
A Channel-Based Pipeline
Chain stages: source worker emits chunks, a transform worker processes them, a sink worker aggregates — each connected by a channel for a clean streaming pipeline.
Quick Check
How do you send a large ArrayBuffer to a worker without copying it?
Recap
You explored message passing with postMessage, transferables to avoid copies, and channel abstractions with request IDs and worker pools. Hybrid designs pair messages for control with shared memory for bulk data.
Frequently asked questions
Is the “Message Passing and Channels Between WASM Threads” lesson free?
Yes — the full text of “Message Passing and Channels Between WASM Threads” is free to read here on the web, and the WebAssembly (WASM) for High Performance Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebAssembly (WASM) for High Performance Apps course, upgrade to CoddyKit PRO.
What will I learn in “Message Passing and Channels Between WASM Threads”?
Coordinate parallel WASM workers using message passing patterns and channel abstractions instead of raw shared memory. You practise WebAssembly (WASM) for High Performance Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebAssembly (WASM) for High Performance Apps?
No prior experience is required. WebAssembly (WASM) for High Performance Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Message Passing and Channels Between WASM Threads” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebAssembly (WASM) for High Performance Apps lesson?
Yes. Every WebAssembly (WASM) for High Performance Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Web Workers with WASM Threads
- SharedArrayBuffer & Atomics for WASM
- Designing Concurrent WASM Applications
- Message Passing and Channels Between WASM Threads