
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
iframe-postmessage
Advanced tools
A robust cross-frame communication library for secure parent-child iframe messaging with enhanced multi-iframe support
A robust, secure, and reliable cross-frame communication library for parent-child iframe messaging with enhanced multi-iframe support
iframe-postmessage is a production-ready library that simplifies secure communication between parent pages and iframe content. Built with TypeScript, zero dependencies, and designed for concurrent multi-iframe scenarios.

npm install iframe-postmessage
yarn add iframe-postmessage
pnpm add iframe-postmessage
import IframePostmessage from 'iframe-postmessage';
// Create a new iframe connection
const connection = await new IframePostmessage({
url: 'https://example.com/child-page.html',
container: document.getElementById('iframe-container'),
classListArray: ['custom-iframe'],
model: {
// Share data/methods with child
doSomething: (data: string) => {
console.log('Child called doSomething with:', data);
},
},
});
// Get a value from child
const value = await connection.get('someProperty');
console.log('Value from child:', value);
// Call a method on child
connection.call('someMethod', { data: 'example' });
// Listen to events from child
connection.on('someEvent', (data) => {
console.log('Event from child:', data);
});
// Clean up when done
// connection.destroy();
import { IframePostmessage } from 'iframe-postmessage';
// Create child model
const connection = await new IframePostmessage.Model({
// Expose methods/properties to parent
someProperty: 'value',
someMethod: (data: unknown) => {
console.log('Parent called someMethod with:', data);
return 'response';
},
});
// Emit events to parent
connection.emit('someEvent', { data: 'example' });
IframePostmessage (Parent)new IframePostmessage(config: IframePostmessageConfig): Promise<ParentAPI>
interface IframePostmessageConfig {
url: string; // URL of the iframe content (required)
container?: HTMLElement; // Container element (default: document.body)
classListArray?: string[]; // CSS classes to add to iframe
title?: string; // iframe title attribute
ariaLabel?: string; // iframe aria-label attribute
name?: string; // iframe name attribute
model?: Record<string, unknown>; // Data/methods to share with child
}
| Method | Description | Returns |
|---|---|---|
get(property: string) | Get a value from the child | Promise<unknown> |
call(property: string, data?: unknown) | Call a method on the child | void |
on(eventName: string, callback) | Listen to events from child | void |
destroy() | Destroy the iframe connection and remove it from DOM | void |
IframePostmessage.Model (Child)new IframePostmessage.Model(model: Record<string, unknown>): Promise<ChildAPI>
| Method | Description | Returns |
|---|---|---|
emit(name: string, data: unknown) | Emit an event to the parent | void |
The library handles multiple concurrent iframe connections automatically:
const [child1, child2, child3] = await Promise.all([
new IframePostmessage({ url: 'https://example.com/iframe1.html' }),
new IframePostmessage({ url: 'https://example.com/iframe2.html' }),
new IframePostmessage({ url: 'https://example.com/iframe3.html' }),
]);
// All three iframes are ready
child1.call('method1');
child2.call('method2');
child3.call('method3');
try {
const connection = await new IframePostmessage({
url: 'https://example.com/child.html',
});
console.log('✅ Connected to child');
} catch (error) {
console.error('❌ Failed to connect:', error);
// Handle handshake failure
}
// Modern async/await syntax
const connection = await new IframePostmessage({
url: 'https://example.com/child.html',
});
const value = await connection.get('property');
connection.call('method', { data: 'value' });
// Parent
connection.on('childReady', (data) => {
console.log('Child is ready!', data);
});
connection.on('dataUpdate', (data) => {
updateUI(data);
});
// Child
connection.emit('childReady', { timestamp: Date.now() });
connection.emit('dataUpdate', { count: 42 });
// Parent shares methods with child
const connection = await new IframePostmessage({
url: 'https://example.com/child.html',
model: {
updateParentState: (newState: any) => {
// Update parent state
setState(newState);
},
getParentConfig: () => {
return { theme: 'dark', lang: 'en' };
},
},
});
// Child can call these methods
// (methods are automatically available in child's model)
Check out the interactive examples in the examples/ directory:

Build the library:
npm run build
Start a local server:
# Recommended: Use the included server (handles ES modules correctly)
node examples/server.js
# Or using Python 3
python3 -m http.server 8000
# Or using Node.js
npx http-server -p 8000
Open the parent example:
http://localhost:8000/examples/parent.html

The examples demonstrate:

See examples/README.md for more details.
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Generate coverage report
npm run test:coverage
# Install dependencies
npm install
# Build the project
npm run build
# Clean build artifacts
npm run clean
Full TypeScript definitions are included. No need to install @types/iframe-postmessage.
import IframePostmessage, {
ParentAPI,
ChildAPI,
IframePostmessageConfig
} from 'iframe-postmessage';
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)MIT © Sangit
Built with ❤️ for the developer community. Special thanks to all contributors!
FAQs
A robust cross-frame communication library for secure parent-child iframe messaging with enhanced multi-iframe support
We found that iframe-postmessage demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.