Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
channel-rpc
Advanced tools
postMessage
Channel-rpc is a TypeScript library that simplifies JSON-RPC communication between different windows or iframes using the postMessage
API. This library is designed to make inter-window communication a breeze, ensuring that only windows with matching channelId
can exchange data and optionally validating the allowed origins.
postMessage
and JSON-RPC.channelId
, and optionally validate the allowed origins.start()
and stop()
methods to manage when the server accepts messages.client.stub
conform to the JSON-RPC standard, including well-defined error codes and messages.To get started with channel-rpc
, you can install it via npm:
npm install channel-rpc
import { ChannelServer } from "channel-rpc";
// Define your message handlers
const handler = {
add: (a: number, b: number): number => a + b,
};
// Create a ChannelServer instance
const server = new ChannelServer({
channelId: "channel-1", // Must match the channelId in the child window
handler: handler, // Your message handler
allowOrigins: ["https://yourwebsite.com", "https://anotherwebsite.com"], // Optional allowed origins
});
// Start accepting messages
server.start();
export type HandlerType = typeof handler;
import { ChannelClient, ChannelErrors } from "channel-rpc";
import type { HandlerType } from "./main.ts";
// Create a ChannelClient instance
const client = new ChannelClient({
target: window.top,
channel: "channel-1", // Must match the channelId in the main window
});
// Use the stub to call methods on the main window
try {
const result = await client.stub.add(2, 3);
} catch (error) {
if (error.code === ChannelErrors.MethodNotFound.code) {
// Handle the "Method not found" error
} else if (error.code === ChannelErrors.InvalidRequest.code) {
// Handle the "Invalid Request" error
}
}
ChannelServer
channelId
(string): A unique identifier for the channel.handler
(object): The message handler object.allowOrigins
(string[], optional): An array of allowed origins.start()
: Starts accepting messages.stop()
: Stops accepting messages.ChannelClient
target
(Window): The target window for communication.channel
(string): The channel identifier to match with the main window.ChannelErrors
InvalidRequest
: Error object representing an "Invalid Request."MethodNotFound
: Error object representing a "Method not found."InternalError
: Error object representing an "Internal error."Timeout
: Error object representing a "Timeout."This project is licensed under the MIT License. See the LICENSE file for details.
To get started with channel-rpc
, follow the installation and usage instructions provided above. Explore the examples folder for more usage examples.
If you encounter any issues or have questions, please open an issue. We're here to help!
Check out the Release Notes for information on the latest updates and features.
Give channel-rpc
a try, and simplify your inter-window communication in web applications. We look forward to your feedback and contributions!
FAQs
JSON-RPC over postMessage
The npm package channel-rpc receives a total of 50 weekly downloads. As such, channel-rpc popularity was classified as not popular.
We found that channel-rpc demonstrated a not healthy version release cadence and project activity because the last version was released 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
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.