API reference › @evolu/common › Worker › MessageChannel
Defined in: packages/common/src/Worker.ts:163
Platform-agnostic MessageChannel.
Creates two entangled ports: keep one and transfer the other (e.g., to a
SharedWorker via postMessage with transfer). Messages sent to one port
are received by the other.
Messages are queued until onMessage is assigned, enabling safe async
initialization. The sender can post messages immediately while the receiver
performs async setup — no manual buffering required.
For one-way communication, omit Output (defaults to never).
Example
Transfer a channel port to a SharedWorker for async initialization:
// Main thread: create channel, transfer port1, use port2 immediately.
const channel = createMessageChannel<EvoluRequest, EvoluResponse>();
sharedWorker.port.postMessage(
{ type: "CreateEvolu", port: channel.port1.native },
[channel.port1.native],
);
// Safe to send immediately — messages queue until worker is ready.
channel.port2.postMessage({ type: "Query", query });
channel.port2.onMessage = (response) => {
handleResponse(response);
};
// Worker: receive the port, do async init, then start listening.
const evoluPort = createMessagePort<EvoluResponse, EvoluRequest>(message.port);
await openDatabase(name);
evoluPort.onMessage = (request) => {
handleRequest(request);
};
// Queued messages are now delivered in order.
See
https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel
Extends
Extended by
Methods
[dispose]()
dispose: void;
Defined in: node_modules/.bun/typescript@5.9.3/node_modules/typescript/lib/lib.esnext.disposable.d.ts:36
Inherited from
Disposable.[dispose]
Properties
port1
readonly port1: MessagePort<Input, Output>;
Defined in: packages/common/src/Worker.ts:165
The first port of the channel.
port2
readonly port2: MessagePort<Output, Input>;
Defined in: packages/common/src/Worker.ts:168
The second port of the channel.