https://github.com/zekiah-a/shared-ipc
A simple JavaScript library providing an asynchronous method call interface for Workers, Iframes and cross-window contexts
https://github.com/zekiah-a/shared-ipc
communication iframe inter-process ipc javascript npm postmessage worker
Last synced: about 2 months ago
JSON representation
A simple JavaScript library providing an asynchronous method call interface for Workers, Iframes and cross-window contexts
- Host: GitHub
- URL: https://github.com/zekiah-a/shared-ipc
- Owner: Zekiah-A
- License: mit
- Created: 2025-04-13T16:12:41.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-04-15T01:13:17.000Z (about 2 months ago)
- Last Synced: 2025-04-15T01:18:12.703Z (about 2 months ago)
- Topics: communication, iframe, inter-process, ipc, javascript, npm, postmessage, worker
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/shared-ipc
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# shared-ipc
A simple JavaScript library providing an asynchronous method call interface for Workers, Iframes and cross-window contexts using postMessage.
## Installation
```bash
npm install shared-ipc
```## Usage
### Basic Setup
```javascript
import {
makeIpcRequest,
sendIpcMessage,
addMessageHandler,
handleMessage
} from "shared-ipc";// In both frames/workers:
window.addEventListener("message", handleMessage);// Add message handlers
addMessageHandler("greet", (name) => {
return `Hello ${name}!`;
});// In parent frame communicating with iframe:
const iframe = document.getElementById("my-iframe");
await makeIpcRequest(iframe, "greet", "World"); // Returns "Hello World!"// Or send a message without waiting for response
sendIpcMessage(iframe, "notification", { type: "alert" });
```## API
### `makeIpcRequest(target:IpcTarget, call:string, data:any)`
Makes an IPC request and returns a promise that resolves with the response.### `sendIpcMessage(target:IpcTarget, call:string, data:any)`
Sends an IPC message without waiting for a response.### `addIpcMessageHandler(name:string, handler:Function)`
Adds a handler function for a specific message type.### `handleIpcMessage(data:IpcEventData)`
The message event handler that processes incoming messages.### `PublicPromise`
Utility class used internally for managing promise resolution.### `IpcTarget` compatibility matrix:
| Type | Window (Browser) | Worker (Browser) | MessagePort (Node) |
|------------------------|------------------|------------------|--------------------|
| **Window (Browser)** | ✅ Yes | ✅ Yes | ❌ No |
| **Worker (Browser)** | ✅ Yes | ✅ Yes | ❌ No |
| **Iframe (Browser)** | ✅ Yes | ✅ Yes | ❌ No |
| **MessagePort (Node)** | ❌ No | ❌ No | ✅ Yes |
| **Worker (Node)** | ❌ No | ❌ No | ✅ Yes |## License
MIT