INTEGRITY Cloudflare Docs

MessageChannel

Background

The MessageChannel API provides a way to create a communication channel between different parts of your application.

The Workers runtime provides a minimal implementation of the MessageChannel API that is currently limited to uses with a single Worker instance. This means that you can use MessageChannel to send messages between different parts of your Worker, but not across different Workers.

const { port1, port2 } = new MessageChannel();

port2.onmessage = (event) => {
	console.log('Received message:', event.data);
};

port2.postMessage('Hello from port2!');

Any value that can be used with the structuredClone(...) API can be sent over the port.

Differences

There are a number of key limitations to the MessageChannel API in Workers: