message-channel-promise
Promise wrapper for communication via post message and MesssageChannel
Installation
npm i message-channel-promise
Usage
The module exposes a function which can be used to wrap communication via MessageChannel in a promise, resolved only when the recepient responds.
This works with IFrames and web workers, and message ports.
parameters
message
: The message to send. Must be a serializable JSON object.target
: ThecontentWindow
or worker. can be omitted if sending from worker, since it sends messages to itself.options
: contains optional parameters (optional):targetOrigin
(optional): The origin to send the message to. Defaults to*
, and not necessary for web workers or message ports.transfer
(optional): a list of other Transferrable objects to be passed.
You should always send a targetOrigin when working with iFrames.
Examples
Usage with IFrames:
const sendChannelMessage = ;const frame = document;const message = /* ... */; ;
Usage with Web Workers:
const sendChannelMessage = ;const worker = someScript; ;
Usage with MessagePort objects:
const sendChannelMessage = ;//get port from somewhere, can also be from other context