post-robot is a JavaScript library that simplifies secure cross-domain communication between browser windows (e.g., parent/iframe, opener/popup) using the native HTML5 `postMessage` API. It provides a robust, promise-based listener/sender pattern, abstracting away the complexities of serialization and asynchronous communication. A key differentiator is its ability to automatically serialize and deserialize complex data types, including functions, Promises (wrapped in `ZalgoPromise`), and Error objects, enabling advanced inter-window interactions. The library ensures reliable messaging with built-in error handling, timeouts, and options for securing channels by specifying target windows or domains. The current stable version is 8.0.32, and while a strict release cadence isn't explicitly stated, it is actively maintained by KrakenJS. This library is crucial for applications requiring seamless interaction between different origins.
npm install post-robotVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up a `post-robot` listener in one window and sending a message to it from another window. It showcases passing data, handling asynchronous responses with Promises, and even invoking functions that were returned across the domain boundary. It includes basic error handling for timeouts.
Be aware that received Promises are `ZalgoPromise` instances. If compatibility with native Promises is critical, manually convert them (e.g., `Promise.resolve(zalgoPromise)`) or adjust checks to account for `ZalgoPromise`.
Always consult the official documentation for your specific `post-robot` version. When upgrading, review the release notes for breaking changes. For example, some configuration options might have changed names or structures.
Always explicitly define the `domain` option on both `on` and `send` calls to match the expected origin for secure and reliable communication. Use `*` sparingly and only when you fully understand the security implications. For same-domain communication, `window.location.origin` can be used.
Handle promise rejections from `postRobot.send` calls to gracefully manage timeouts. Adjust the `timeout` option (e.g., `postRobot.send(window, 'msg', data, { timeout: 5000 })`) if you expect longer processing times on the receiving end.Ensure `import postRobot from 'post-robot';` (ESM) or `const postRobot = require('post-robot');` (CommonJS) is at the top of your file where `postRobot` is used. Verify your bundler configuration if using a module system.Check the listening window for errors or long-running operations. Increase the `timeout` option in the `postRobot.send` call if a longer processing time is expected on the receiver side (e.g., `{ timeout: 10000 }`). Ensure the listener function actually returns a value or a Promise that resolves.`post-robot` is designed to *circumvent* this exact problem for message passing. Ensure you are using `postRobot.send()` and `postRobot.on()` correctly for communication, and not attempting direct DOM manipulation or property access across origins. Also, verify that the `domain` option in `postRobot.on` and `postRobot.send` is correctly configured.
Review the `domain` (and `window`) option provided to `postRobot.on`. Ensure it correctly specifies the expected origin(s) from which messages should be accepted. If messages are expected from multiple specific origins, the `domain` option can often accept an array of strings.
No dependency data recorded yet.