Port+ is an upgrade to the web's port-based messaging APIs — MessagePort, MessageChannel, BroadcastChannel – and an onboarding of the
WebSocket API into the same port-based messaging model.
This README takes you from installation to the design concepts and, ultimately, to the added capabilities implied by Port+.
npm i @webqit/port-plusimport { MessageChannelPlus, BroadcastChannelPlus, WebSocketPort, StarPort, RelayPort, Observer } from '@webqit/port-plus';<script src="https://unpkg.com/@webqit/port-plus/dist/main.js"></script>
<script>
const { MessageChannelPlus, BroadcastChannelPlus, WebSocketPort, StarPort, RelayPort, Observer } = window.webqit;
</script>Port+ is an API mirror of the Web Messaging APIs built for advanced use cases. An instance of BroadcastChannelPlus, for example, is the same BroadcastChannel instance, but one that lets you do more.
To see that changed, here is the existing set of Web Messaging APIs. Next, is the Port+ equivalent.
MessageChannel (mch)
├─ mch.port1 ──► MessageEvent (e) ──► e.ports
└─ mch.port2 ──► MessageEvent (e) ──► e.ports
In this structure:
mch.port1andmch.port2are each a message port (MessagePort)- messages (
e) arrive asmessageevents (MessageEvent) e.portsare each a message port (MessagePort)
BroadcastChannel (brc) ──► MessageEvent (e)
In this structure:
- the
BroadcastChannelinterface is the message port – the equivalent ofMessagePort - messages (
e) arrive asmessageevents (MessageEvent) - no reply ports;
e.portsis empty; not implemented in BroadcastChannel
WebSocket ──► MessageEvent (e)
In this structure:
- the
WebSocketinterface is partly a message port (havingaddEventListener()) and partly not (nopostMessage()) - messages (
e) arrive asmessageevents (MessageEvent) - no reply ports;
e.portsis empty; not implemented in WebSocket - no API parity with
MessagePort/BroadcastChannelin all
MessageChannelPlus (mch)
├─ mch.port1+ ──► MessageEventPlus (e) ──► e.ports+
└─ mch.port2+ ──► MessageEventPlus (e) ──► e.ports+
In this structure:
mch.port1+andmch.port2+are Port+ interfaces (MessagePortPlus)- messages arrive as
MessageEventPlus e.ports+recursively expose Port+ interfaces- reply ports support advanced features (requests, live objects, relays)
BroadcastChannelPlus (brc) ──► MessageEventPlus (e) ──► e.ports+
In this structure:
BroadcastChannelPlusacts as a full Port+ interface- messages arrive as
MessageEventPlus e.ports+enables reply channels where native BroadcastChannel does not- broadcast semantics are preserved while extending capabilities
WebSocketPort ──► MessageEventPlus (e) ──► e.ports+
In this structure:
WebSocketPortwraps aWebSocketas a Port+ interfacepostMessage()replaces ad-hocsend()usage- messages arrive as
MessageEventPlus e.ports+enables reply channels over WebSockets- lifecycle and messaging semantics align with other Port+ interfaces
Port+ unifies the messaging model across all three and extends the port interfaces and MessageEvent interface for advanced use cases.
General mental model:
port+ ──► MessageEventPlus ──► e.ports+
Meaning: Port+ interfaces emit MessageEventPlus, which recursively exposes Port+ interface over at e.ports.
| API / Feature | Port+ | Msg. Ports | WS |
|---|---|---|---|
postMessage() |
✓ (advanced) | ✓ (basic) | ✗ (send()) |
postRequest() |
✓ | ✗ | ✗ |
addEventListener() / onmessage |
✓ | ✓ | ✓ |
addRequestListener() |
✓ | ✗ | ✗ |
readyState |
✓ | ✗ | ✓ |
readyStateChange() |
✓ | ✗ | ✗ |
relay() |
✓ | ✗ | ✗ |
channel() |
✓ | ✗ | ✗ |
projectMutations() |
✓ | ✗ | ✗ |
close() |
✓ | ✓ | ✓ |
Live Objects** |
✓ | ✗ | ✗ |
In this table:
- Port+ →
MessagePortPlus,BroadcastChannelPlus,WebSocketPort - Msg. Ports →
MessagePort,BroadcastChannel - WS →
WebSocket **→ All-new concept
| API / Feature | Port+ | Msg. Event | WS |
|---|---|---|---|
data |
✓ (Live Objects support) | ✓ (no Live Objects) | ✓ (typically string) |
type |
✓ | ✓ | ✓ |
ports |
✓ (Port+) | ✓** | ✗** |
preventDefault() |
✓ | ✓ | ✗** |
defaultPrevented |
✓ | ✓ | ✗** |
stopPropagation() |
✓ | ✓ | ✗** |
stopImmediatePropagation() |
✓ | ✓ | ✗** |
respondWith() |
✓ | ✗ | ✗ |
eventID |
✓ | ✗ | ✗ |
live |
✓ | ✗ | ✗ |
relayedFrom |
✓ | ✗ | ✗ |
In this table:
- Port+ →
MessageEventPlus - Msg. Event →
MessageEvent - WS →
WebSocket'sMessageEvent **→ May be present, but may not be implemented
The APIs below are the entry points to a Port+-based messaging system.
const mch = new MessageChannelPlus();
const brc = new BroadcastChannelPlus('channel-name');
const soc = new WebSocketPort(url); // or new WebSocketPort(ws)Above, WebSocketPort also takes a WebSocket instance – letting you create a port from an existing WebSocket connection:
const ws = new WebSocket(url);
const port = new WebSocketPort(ws);On a WebSocket server, for example, you can do:
const wss = new WebSocketServer({ server });
wss.on('connection', (ws) => {
// The basic way
ws.send('something');
// The unified way
const port = new WebSocketPort(ws);
port.postMessage('something');
});Whatever the port+ type, every Port+ instance exposes the same interface and capabilities. For example, with WebSocketPort you get an event.ports implementation over web sockets consistent with the rest.
All Port+ interfaces also support live state projection, lifecycle coordination, request/response semantics, and routing.
Port+ extends message passing with the ability to project state across a port connection and keep that state synchronized over time.
This capability is referred to as Live State Projection, and the projected objects are called Live Objects.
Live State Projection is established via the same .postMessage() API:
Sender:
const state = { count: 0 };
port.postMessage({ state }, { live: true });Receiver:
port.addEventListener('message', (e) => {
if (e.live) console.log('Live object received');
const { state } = e.data;
});In live mode, continuity of the original object is achieved. Every mutation on the sender side automatically converges on the received copy, and those mustations are observable:
Sender:
setInterval(() => {
Observer.set(state, 'count', state.count + 1;
}, 1000);Receiver:
Observer.observe(state, () => {
console.log(state.count);
});When an object is sent with { live: true }, Port+ establishes a projection with the following behavior:
- mutations on the source object are observed using using the Observer API
- differential updates are sent over a private channel; they converge on the same object on the other side
Projection is bound to the lifecycle of the port. It begins once the message is delivered and terminates automatically when the port closes. After closure, the target object remains usable but no longer receives updates.
In some cases, live state must be projected independently of a specific message. Port+ supports this through a .projectMutations() API.
Instead of deriving identity from a message event, both sides explicitly agree on a shared object identity and a propagation channel over which to project mutations.
Below, both sides agree on a certain object identity – 'counter' – and a propagation channel anique to the object: 'counter'.
Sender
const state = { count: 0 };
const stop = port.projectMutations({
from: state,
to: 'counter'
});
setInterval(() => {
Observer.set(state, 'count', state.count + 1);
}, 1000);Receiver:
const state = {};
const stop = port.projectMutations({
from: 'counter',
to: state
});
Observer.observe(state, () => {
console.log(state.count);
});In each case, the return value of projectMutations() is a cleanup function:
stop(); // terminates the projectionCalling it stops mutation tracking and synchronization without closing the port.
This lower-level API is intended for advanced scenarios where object identity and lifetime are managed outside the messaging system.
Live State Projection enables a shared reactive model across execution contexts.
Rather than exchanging updated values, both sides operate on corresponding objects that maintain:
- shared identity — distinct objects representing the same logical entity
- continuity — stable object references over time
- deterministic convergence — ordered, differential mutation application
- lifecycle scoping — synchronization exists only while the port exists
This allows state to be treated as persistent and reactive across a messaging boundary, without polling, replacement, or manual reconciliation.
This section covers important phases in the life of a Port+ instance. Understanding Port+ lifecycles is recommended for coordinating a Port+ messaging system.
Every Port+ instance transitions through different states in its lifetime:
connecting: The port is being established or is waiting for a connection to be established.open: The port is ready for interaction.closed: The port is closed.
At any given point in time, a port is in exactly one of these states. This state is exposed via the .readyState property.
State transitions (observable milestones) can be observed imperatively using .readyStateChange():
// The port is ready for interaction.
await port.readyStateChange('open');
// The port has sent its first message.
await port.readyStateChange('messaging');
// The port is closed.
await port.readyStateChange('close');Tip
The readyState property reflects the current state as a descriptive value (e.g. 'closed'), while readyStateChange() listens for lifecycle transitions using event-style names (e.g. 'close').
Native web messaging APIs do not expose Ready State information consistently. While WebSockets expose transport-level Ready State (connecting -> open -> closing -> closed), MessagePorts and BroadcastChannels expose no readiness signal at all. Consequently, the default Ready State behaviour acorss port types is:
| Port Type | Ready State |
|---|---|
WebSocket |
open (when a connection is established) -> messaging (when the first message is sent) -> close (when the connection is closed) |
MessagePort |
open (immediately on instantiation) -> messaging (when the first message is sent) -> close (when the port is closed) |
BroadcastChannel |
open (immediately on instantiation) -> messaging (when the first message is sent) -> close (when the channel is closed) |
Here, only WebSockets can tell when the other side of the port is connected. For the others, the open state is really about the local end of the port itself, not about the remote end.
Not knowing when the other side of the port is connected can be a limitation when multiple lifecycles need to be coordinated.
Port+ addresses this by introducing an explicit handshake phase that applies uniformly across all port types. You opt-in via options.handshake:
// An example for a BroadcastChannel port
const port = new BroadcastChannel(channel, { handshake: 1 });options.handshake is a number between 0 and 2. When 0 – the default – no handshake takes place. When 1 or 2, the port goes through the Port+ handshake phase.
Port+'s handshake model is designed to guarantee the readiness of the remote end of the port – rather than the readiness of the local end itself. In this mode, the port only transitions to the open state after each end of the port has ascertained that the other end is ready to interact – not just alive. Messages sent at this point are more likely to be read by "someone".
Either end begins the process by sending a "readinnes" signal and waits for it to be acknoledged. If the other end also acknoledges with "readinnes", both ends transitions to the open state. Otherwise, the originating end waits for an explicit "readinnes" signal from the other end. The transition to the open state happens when both ends have successfully exchanged "readinnes".
A Port+ instance self-signifies "readiness" on exactly one condition: when .start() is called. It says by that: "I'm ready to interact, not just alive". The options.handshake parameter, however, lets you say that either explicitly or implicitly:
- When
1, handshake begins on the first interaction with:addEventListener()– including higher level APIs that may trigger itpostMessage()– including, also, higher level APIs that may trigger it This behaviour is called Readinnes by Interaction.
- When
2, handshake begins on an explicit call tostart()
// An example for a BroadcastChannel port
const port = new BroadcastChannel(channel, { handshake: 1 });
port.addEventListener('message', handle); // Implicitly triggers start()Ports may be configured to implicitly await the open Ready State before sending messages. In this mode, outbound messages are automatically queued until the port is open.
// An example for a BroadcastChannel port
const port = new BroadcastChannel(channel, { handshake: 1, postAwaitsOpen: true });
port.postMessage('hello');
// Queued until port is open.
// But { handshake: 1 } also lets postMessage() trigger the handshake process.
// Port is open – and messages flush – when the other end says "ready". Until then, queued
await port.readyStateChange('open');
// delivered by nowThis allows application code to send messages with guaranteed coordination.
Each Port+ transport participates in the handshake model differently, while exposing the same observable states.
MessagePorts follow a symmetric, point-to-point handshake.
The port transitions to the open state when:
.start()is triggered explicitly or by interaction – on both ends- peer acknowledgment is recieved
The port closes via an explicit .close() call on either side. Closure on one end automatically triggers closure on the other – via a control message. Ready State transitions to closed.
BroadcastChannels form a many-to-many port topology and require additional coordination to make readiness meaningful.
Port+ supports two modes.
In default mode, each participant becomes open when:
.start()is triggered explicitly or by interaction- an acknowledgment is recieved from at least one peer in the shared channel
A participant closes via an explicit .close() call. Its Ready State transitions to closed.
While readiness is synchronized in the default mode – as with other port types – closure is not. A participant's closure has no effect on the others, by default.
To support use cases that require synchronized closure across participants, Port+ introduces an optional client/server operational model for BroadcastChannels.
The client/server model establishes explicit role semantics. Here, one participant is assigned a server role – the "control plane" – and the others are assigned a client role:
const server = new BroadcastChannelPlus('room', {
clientServerMode: 'server'
});
const client1 = new BroadcastChannelPlus('room', {
clientServerMode: 'client'
});
const client2 = new BroadcastChannelPlus('room', {
clientServerMode: 'client'
});Both server and clients can join the channel in any order, but the server:
- maintains a reference to all connected clients
- automatically triggers closure across all clients when closed
- automatically closes when all clients leave – but if
options.autoCloseis enabled
By contrast, a client:
- closes alone when closed
This mode exists because BroadcastChannel’s native semantics do not provide coordinated teardown or authoritative control. Without it, participants cannot reliably know when a session has ended. Client/server mode enables explicit ownership, deterministic shutdown, and presence-aware coordination over a many-to-many topology.
WebSockets have a native Ready State system inspectable via an instance's .readyState property.
By default, Port+ lets the WebSocket’s native Ready State be the authoritative Ready State.
In this mode:
- The WebSocketPort's
openandclosedstates are based on the WebSocket's nativeopenandclosedstates - no handshake takes place
- readiness is assumed once the socket opens
On choosing the explicit handshake model via options.handshake, the open state is determined differently.
const port = new WebSocketPort(ws, { handshake: 1 });In this mode, each side transitions to the open state when:
.start()is triggered explicitly or by interaction – on both ends- peer acknowledgment is recieved
This allows WebSocketPort to behave identically to MessagePortPlus and BroadcastChannelPlus with respect to readiness and cleanup.
Port+ is not limited to point-to-point messaging. Ports can be composed into higher-level structures that define how messages flow, where they propagate, and which connections participate.
A StarPort is a fan-in / fan-out proxy over multiple ports.
It acts as a central aggregation point where:
- messages received by child ports bubble up to the star
- messages sent by the star fan out to all child ports
const star = new StarPort();
star.addPort(port1);
star.addPort(port2);
star.addPort(port3);As with every port, when a connected port receives a message from its remote peer, it receives a MessageEventPlus. Internally that comes as an event dispatched on the port:
port1.dispatchEvent(new MessageEventPlus(data));The event:
- is dispatched on
port1 - bubbles up to the
StarPort; thus, re-dispatched onstar
The star port essentially makes it possible to listen to all messages received by any of its child ports:
star.addEventListener('message', (e) => {
// receives messages from any child port
});A .start() call on the Star Port is a .start() call on all connected ports.
A .close() call on the Star Port is a .close() call on all connected ports.
A .postMessage() call on the Star Port is a .postMessage() call on all connected ports.
star.postMessage(data);This makes StarPort a true proxy: a single observable endpoint over many independent ports.
A Star Port automatically transitions to the open state by default. But when options.handshake is enabled, its Ready State is controlled by that of its child ports:
- transitions to the
openstate when at least a child port exists and has Ready Stateopen - transitions to the
closedstate when the last child closes or is removed – but ifoptions.autoCloseis enabled
Regardless of options.handshake:
- Closed ports are removed automatically.
- Child ports added after
.start()are automatically started. - Attempting to add a child port after
.close()fails with an error.
- centralized coordination
- shared state distribution
- transport-agnostic hubs
A RelayPort is a router that forwards messages between sibling ports.
It is an extension of StarPort and inherits all of its properties, methods, and lifecycle behavior.
const relay = new RelayPort('room');
relay.addPort(port1);
relay.addPort(port2);
relay.addPort(port3);As with every port, when a connected port receives a message from its remote peer, it receives a MessageEventPlus. Internally that comes as an event dispatched on the port:
port1.dispatchEvent(new MessageEventPlus(data));Bubbling behaviour works as with a star port. In addition, the relay forwards it to all other connected ports as a .postMessage() call – excluding the originating port.
Each connected port sees the message as if it were sent directly by its peer.
This creates peer-to-peer fan-out.
As with the Star Port, a .postMessage() call on the relay port is a .postMessage() call to all connected ports.
When a port joins (via relay.addPort()) or leaves (via relay.removePort() or via port closure), a synthetic join/leave message is routed to peers.
This enables presence-aware systems (e.g. chat rooms).
- chat rooms
- collaborative sessions
- event fan-out
- decoupled peer coordination
port.channel() is a universal instance method on all port types that creates a logical sub-port scoped to a message type or namespace over that port.
const chat = port.channel('chat');
const system = port.channel('system');A channel:
- filters inbound messages by the specified namespace (e.g.
chatabove) - automatically namespaces outbound messages with the same
chat.postMessage({ text: 'hello' });
// Equivalent to:
chat.postMessage({ text: 'hello' }, { type: 'chat:message' });chat.addEventListener('message', (e) => {
// receives only 'chat' messages
});
// Equivalent to:
chat.addEventListener('chat:message', (e) => {
// handle 'chat' messages
});Channels are namespaces within the same port.
Channels compose naturally with StarPort and RelayPort.
port.relay() is a universal instance method on all port types that establishes explicit routing relationships between ports.
portA.relay({
to: portB,
channel: 'chat',
bidirectional: true
});This means:
- messages received by
portAon channelchat→ are forwarded toportB - optionally in both directions
- respecting lifecycle, and teardown rules – relay relationships are automatically torn down when either port closes
Use port.relay() to chain ports together transparently.
// Forward Port A -> Port B
portA.relay({ to: portB });
// Bidirectional A <-> B
portA.relay({ to: portB, bidirectional: true });Use RelayPort when:
- routing is shared across many ports
- join/leave semantics matter
- topology is explicit
Live objects propagate through composition transparently.
- events bubble, or route, as defined
- mutation convergence follows routing paths
- projection terminates when required links close
This allows shared reactive state to exist across entire topologies, not just between endpoints.
Port+ supports a small number of messaging patterns. These patterns are not separate APIs — they are ways of structuring interactions using the same port abstraction.
Use this pattern when you need to notify the other side, without waiting for a response.
port.postMessage({ op: 'invalidate-cache' });This is appropriate when:
- ordering matters, but acknowledgment does not
- the sender does not depend on the receiver’s result
- failure can be handled independently
Use this pattern when the sender expects a result and wants deterministic correlation.
const result = await port.postRequest({
op: 'multiply',
args: [6, 7]
});On the receiving side:
port.addEventListener('request', (e) => {
if (e.data.op === 'multiply') {
return e.data.args[0] * e.data.args[1];
}
});This pattern provides:
- automatic request correlation
- promise-based control flow
- rejection on timeout or port closure
Use this for command execution, queries, and remote procedure calls.
Some interactions are not single exchanges, but conversations.
In these cases, Port+ provides reply ports — temporary, private ports scoped to a specific message. This is what .postRequest() and .addRequestListener() do under the hood.
const messageChannel = new MessageChannelPlus;
// Listen on the reply port
messageChannel.port2.addEventListener('message', (e) => {
// handle reply
console.log('reply', e.data);
});
// Send the message with the reply port
const result = await port.postRequest(
{
op: 'multiply',
args: [6, 7]
},
[messageChannel.port1] // Transfer the reply port
);On the receiving side:
port.addEventListener('message', (e) => {
const reply = e.ports[0];
reply.postMessage(e.data.args[0] * e.data.args[1]);
// Continue the conversation
reply.addEventListener('message', (e) => {
console.log('follow-up:', e.data);
});
});Reply ports:
- form a symmetric 1:1 connection
- close automatically when either side closes
Use reply ports when:
- responses are multi-step
- data streams over time
- isolation from other traffic matters
Use channels to separate independent flows over the same port. Channels also use reply ports under the hood.
const chat = port.channel('chat');
const system = port.channel('system');chat.postMessage({ text: 'hello' });
system.postMessage({ action: 'sync' });Channels provide:
- logical namespacing
- inbound filtering
- outbound tagging
They do not create new connections and compose naturally with routing and topology.
Use this pattern when two sides must stay synchronized over time.
port.postMessage(state, { live: true });On the receiving side:
port.addEventListener('message', (e) => {
if (e.live) {
Observer.observe(e.data, () => {
render(e.data);
});
}
});This pattern enables:
- shared identity across contexts
- differential mutation propagation
- lifecycle-bound reactivity
It is the foundation for collaborative state, projections, and reactive coordination.
(Detailed semantics are covered in the Live Objects section.)
This section defines the formal API contract for Port+. Conceptual behavior, lifecycle semantics, and usage patterns are documented in earlier sections.
MessagePortPlus(Base & Concrete Interfaces)MessageEventPlus
All Port+ implementations – MessagePortPlus, BroadcastChannelPlus, WebSocketPort, StarPort, RelayPort – conform to the MessagePortPlus interface.
MessageChannelPlusBroadcastChannelPlusWebSocketPortStarPortRelayPort
new MessageChannelPlus({
handshake?: number,
postAwaitsOpen?: boolean
});| Option | Default | Description |
|---|---|---|
handshake |
0 |
Conduct a handshake process to coordinate Ready State |
postAwaitsOpen |
false |
Queue messages until the port is open |
new BroadcastChannelPlus(name, {
handshake?: number,
postAwaitsOpen?: boolean,
clientServerMode?: 'server' | 'client' | null,
autoClose?: boolean
});| Option | Default | Description |
|---|---|---|
handshake |
0 |
Conduct a handshake process to coordinate Ready State |
postAwaitsOpen |
false |
Queue messages until readiness |
clientServerMode |
null |
Can be one of 'server', 'client' or null |
autoClose |
false |
Auto-close server when all clients disconnect |
new WebSocketPort(wsOrUrl, {
handshake?: number,
postAwaitsOpen?: boolean
});| Option | Default | Description |
|---|---|---|
handshake |
0 |
Conduct a handshake process to coordinate Ready State |
postAwaitsOpen |
false |
Queue messages until readiness |
new StarPort({
handshake?: number,
postAwaitsOpen?: boolean,
autoClose?: boolean
});| Option | Default | Description |
|---|---|---|
handshake |
0 |
Conduct a handshake process to coordinate Ready State |
postAwaitsOpen |
false |
Queue messages until readiness |
autoClose |
false |
Auto-close when all clients close or are removed |
new RelayPort(channelSpec?, {
handshake?: number,
postAwaitsOpen?: boolean,
autoClose?: boolean
});| Option | Default | Description |
|---|---|---|
handshake |
0 |
Conduct a handshake process to coordinate Ready State |
postAwaitsOpen |
false |
Queue messages until readiness |
autoClose |
false |
Auto-close when all clients close or are removed |
start()close()readyStatereadyStateChange()
Explicitly initiates the interaction handshake.
- Required when
options.handshakeis2 - Signals readiness to the remote side
Closes the port.
- Sends a control close signal
- Triggers teardown of dependent ports and projections
- Transitions
readyStatetoclosed
Current lifecycle state of the port.
Resolves when the port transitions into the specified state.
'open': handshake completed'messaging': first outbound message sent'close': port fully closed
Tip
This is the recommended way to listen for port lifecycle changes compared to using addEventListener('close', ...).
While a corresponding open and close events are dispatched at the same time as the readyStateChange promise, those events may also be simulated manually via dispatchEvent() or postMessage(). By contrast, readyStateChange is system-managed.
Code that depends on port lifecycle changes should always rely on readyState or readyStateChange().
postMessage()postRequest()addEventListener()addRequestListener()
Sends a message over the port. options are:
| Option | Type | Description |
|---|---|---|
type |
string | Message event type (default: 'message') |
live |
boolean | Enable live object projection |
transfer |
Array | Transferable objects |
Sends a request and awaits a response. options are:
| Option | Type | Description |
|---|---|---|
timeout |
number | Reject if no response within duration |
signal |
AbortSignal | Abort the request |
- Rejects automatically if the port closes
- Uses a private ephemeral reply channel internally
Registers a handler for a specific message type. options are:
| Option | Type | Description |
|---|---|---|
once |
boolean | Remove listener after first invocation |
signal |
AbortSignal | Abort the request |
Receives MessageEventPlus instances.
Registers a request handler for a specific message type. options are:
| Option | Type | Description |
|---|---|---|
once |
boolean | Remove listener after first invocation |
signal |
AbortSignal | Abort the request |
- Return value (or resolved promise) is sent as the response
- Thrown errors reject the requester
channel()relay()
Creates a logical sub-port scoped to a message namespace.
- Filters inbound messages
- Tags outbound messages
- Shares lifecycle with the parent port
Establishes explicit routing between ports. config is:
| Config Option | Type | Description |
|---|---|---|
to |
MessagePortPlus | Target port |
channel |
string | object |
bidirectional |
boolean | Relay in both directions |
resolveMessage |
function | Transform message payload |
If channel is specified, it may be a string or a { from, to } mapping. Only messages of the specified channel are relayed. If specified as a from -> to mapping, incoming messages are matched for from and outgoing messages are namespaced with to – effectively a channel mapping.
Returns a cleanup function that removes the relay.
projectMutations()
Projects mutations between objects across a port. options are:
| Option | Type | Description |
|---|---|---|
from |
object | string |
to |
string | object |
- Must be called on both ends with complementary
from/to - Returns a function that terminates the projection
All message events dispatched by Port+ ports.
| Property | Type | Description |
|---|---|---|
data |
any | Message payload |
type |
string | Event type |
eventID |
string | Stable message identifier |
ports |
MessagePortPlus[] |
Reply ports. |
live |
boolean | Indicates a live object payload |
relayedFrom |
MessagePortPlus |
Originating port (if routed) |
| Method | Description |
|---|---|
respondWith() |
Sends a response through attached reply ports. data and options are as is with postMessage. |
Sends a response through attached reply ports. data and options are as is with postMessage.
Returns true if one or more reply ports were present.
MIT.