npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

channel-rpc

v0.2.6

Published

JSON-RPC over postMessage

Downloads

229

Readme

channel-rpc: JSON-RPC over postMessage

GitHub License

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.

Features

  • Simple and Lightweight: A minimalistic library that abstracts away the complexities of postMessage and JSON-RPC.
  • Strongly Typed: Leverages TypeScript to ensure type safety in your communications.
  • Secure: Channels can only communicate with windows or iframes having the same channelId, and optionally validate the allowed origins.
  • Controlled Communication: Utilize the start() and stop() methods to manage when the server accepts messages.
  • Error Handling: Error responses from client.stub conform to the JSON-RPC standard, including well-defined error codes and messages.

Installation

To get started with channel-rpc, you can install it via npm:

npm install channel-rpc

Usage

In the Main Window (main.ts)

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;

In the Child Window (iframe.ts)

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
  }
}

API Reference

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."

License

This project is licensed under the MIT License. See the LICENSE file for details.

Get Started

To get started with channel-rpc, follow the installation and usage instructions provided above. Explore the examples folder for more usage examples.

Issues and Support

If you encounter any issues or have questions, please open an issue. We're here to help!

Release Notes

Check out the Release Notes for information on the latest updates and features.

Author

Give channel-rpc a try, and simplify your inter-window communication in web applications. We look forward to your feedback and contributions!