# @solid-primitives/post-message
[](https://primitives.solidjs.community/package/post-message)
[](https://www.npmjs.com/package/@solid-primitives/post-message)
[](https://github.com/solidjs-community/solid-primitives#contribution-process)
[](https://vitest.dev)
Secure messaging between windows, iframes and `MessagePort`s, with origin, source and channel checks. Built for cases like an editor talking to a preview iframe.
- **`makePostMessage`** — non-reactive channel; call `close()` to clean up.
- **`createPostMessage`** — reactive channel; the target can be an accessor, it is torn down with the owner, and it returns inert stubs on the server.
Security defaults:
- Messages from a window or iframe are accepted only when `event.source` is the target window **and** `event.origin` is in `allowedOrigins`. An empty list accepts nothing; `"*"` only applies if you list it.
- Sending to a window requires an explicit `targetOrigin`. There is no `"*"` default; omitting it throws a `TypeError` (and is a type error).
- `validate` drops anything that is not the shape you expect and narrows the type of incoming data.
- `channel` wraps outgoing data in an envelope and ignores messages that do not carry the same channel, so unrelated traffic never reaches your handlers.
- `MessagePort`s have no origin; the port itself is the trust boundary. `close()` stops listening but does not close the port.
## Installation
```bash
npm install @solid-primitives/post-message
# or
yarn add @solid-primitives/post-message
# or
pnpm add @solid-primitives/post-message
```
## `makePostMessage`
```ts
// Type
function makePostMessage(
target: Window | HTMLIFrameElement,
options: {
allowedOrigins: readonly string[];
targetOrigin: string;
validate?: (data: unknown) => data is TIn;
channel?: string;
receiver?: Window; // where "message" events arrive, defaults to window
},
): {
send: (data: TOut, transfer?: Transferable[]) => boolean;
onMessage: (listener: (data: TIn, event: MessageEvent) => void) => () => void;
close: () => void;
};
// MessagePort targets: options are optional and only validate/channel apply
function makePostMessage(target: MessagePort, options?: { validate?; channel? }): /* same */;
// Example
const origin = "https://preview.example.com";
const channel = makePostMessage<{ type: "ready" }, { code: string }>(iframe, {
allowedOrigins: [origin],
targetOrigin: origin,
channel: "editor",
validate: (d): d is { type: "ready" } => (d as any)?.type === "ready",
});
channel.onMessage(msg => console.log(msg.type));
channel.send({ code: "console.log(1)" });
channel.close();
```
An iframe is resolved to its `contentWindow` each time it is needed, so navigations and late loads work. `send` returns `false` when there is nothing to send to (closed handle, iframe without a window).
## `createPostMessage`
```ts
// Type
function createPostMessage(
options: /* same options as makePostMessage */ & {
target: MaybeAccessor;
},
): {
message: Accessor; // latest accepted message
send: (data: TOut, transfer?: Transferable[]) => boolean;
onMessage: (listener: (data: TIn, event: MessageEvent) => void) => () => void;
close: () => void;
};
// Example
const [iframe, setIframe] = createSignal();
const { send, message } = createPostMessage({
target: iframe,
allowedOrigins: [origin],
targetOrigin: origin,
validate: isReply,
});
;
createEffect(message, reply => console.log(reply));
```
The channel re-attaches whenever `target` changes. The `message` signal updates on every accepted message, even when the payload is identical.
## Changelog
See [CHANGELOG.md](./CHANGELOG.md)