Skip to content

A simple JavaScript library providing an asynchronous method call interface for Workers, Iframes and cross-window contexts

License

Notifications You must be signed in to change notification settings

Zekiah-A/shared-ipc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shared-ipc

A simple JavaScript library providing an asynchronous method call interface for Workers, Iframes and cross-window contexts using postMessage.

Installation

npm install shared-ipc

Usage

Basic Setup

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

About

A simple JavaScript library providing an asynchronous method call interface for Workers, Iframes and cross-window contexts

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published