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

cross-frame-messenger

v2.0.0

Published

A flexible and easy-to-use library for enabling cross-frame communication in web applications. Supports TypeScript and offers customizable options for robust messaging across different window contexts.

Downloads

8

Readme

CrossFrameMessenger

CrossFrameMessenger is a robust and efficient TypeScript/JavaScript library designed to simplify cross-frame communication in web development. The library provides a set of APIs that allow seamless message transmission between different frames (i.e., windows or iframes) in a web application.

Features

  • Easy-to-use APIs for sending and receiving messages across frames.
  • Message confirmation capability for reliable communication.
  • Debug mode for troubleshooting and development.
  • Highly customizable, supports communication over specified channels.

Installation

CrossFrameMessenger can be installed using npm:

npm install cross-frame-messenger

Please note that the CrossFrameMessenger class should be installed and used on both the sender and receiver ends for efficient and smooth communication.

Usage

To use the CrossFrameMessenger, you'll need to import it in your TypeScript/JavaScript file:

import { CrossFrameMessenger } from 'cross-frame-messenger';

You can import the class and types from the package if needed like so:

import { CrossFrameMessenger, MessageCallback, ConfirmationCallback } from 'cross-frame-messenger';

Instantiation

To create a new instance of CrossFrameMessenger, you need to specify the target window and the target origin. Optionally, you can provide a configuration object:

const messenger = new CrossFrameMessenger(targetWindow, targetOrigin, options);
  • targetWindow: The window object of the target frame.
  • targetOrigin: The origin URL of the target frame.
  • options: An optional configuration object which can include:
    • name: A custom name for the messenger instance (defaults to the document title).
    • channel: A custom channel for the communication (a prefix for the event types).
    • debug: A boolean indicating whether to enable debug mode (default is false).

Methods

Here are some of the main methods provided by the CrossFrameMessenger class:

  • listen(): Start listening for messages.
  • stop(): Stop listening for messages.
  • on(eventTypes, callback): Register a callback function for one or more event types. The callback function receives the data sent with the event and a confirm function. The confirm function can be used to send a confirmation back to the message sender. It accepts two parameters: a boolean indicating the success of the operation, and an optional data object.
  • send(eventType, data, isConfirmation, confirmationId, event): Send a message of a certain event type.
  • sendWithConfirmation(eventType, data): Send a message and expect a confirmation response. This method returns a Promise that resolves with the data from the confirmation if successful, or rejects with the error data if not.

For more details, please refer to the comments in the TypeScript file.

Examples

Let's consider an example where we have two frames, a "Sender" frame and a "Receiver" frame. Both of these frames need to have an instance of CrossFrameMessenger installed and properly configured.

In the Sender frame

import { CrossFrameMessenger } from 'cross-frame-messenger';

// Let's assume that the targetWindow is the reference to the Receiver frame's window
const senderMessenger = new CrossFrameMessenger(targetWindow, targetOrigin, { name: 'Sender' });

// Send a 'greetings' message without expecting any confirmation
senderMessenger.send('greetings', { message: 'Hello, there!' });

// Send a 'request' message expecting a confirmation response
senderMessenger.sendWithConfirmation('request', { item: 'Some data' })
    .then(data => {
        console.log("Request was successfully received! Response:", data);
    })
    .catch(error => {
        console.log("Something went wrong. Error:", error);
    });

In the Receiver frame

import { CrossFrameMessenger } from 'cross-frame-messenger';

// Let's assume that the targetWindow is the reference to the Sender frame's window
const receiverMessenger = new CrossFrameMessenger(targetWindow, targetOrigin, { name: 'Receiver' });

// Listen for 'greetings' messages
receiverMessenger.on('greetings', (data) => {
    console.log("Received greetings:", data.message);
});

// Listen for 'request' messages and respond with a confirmation
receiverMessenger.on('request', (data, confirm) => {
    console.log("Received request for:", data.item);
    // process the request here...

    // Then send a confirmation response back
    confirm(true, { response: 'Request processed!' });
});

This example illustrates a typical use-case of the CrossFrameMessenger class where two frames are communicating with each other via messages. The Sender frame sends a greetings message and a request message that expects a confirmation. The Receiver frame listens for these messages, processes the request, and sends a confirmation back.

In the 'request' event callback, the confirm function is used to send a confirmation back to the sender of the message. This function takes two arguments: a boolean indicating success or failure, and an optional data object containing any additional information you want to send back with the confirmation. This is useful when the sender needs to know if the processing of the message was successful or if there was an error.

Contribution

This project is open source, and contributions are very welcome! If you're interested in improving CrossFrameMessenger, feel free to create a pull request.

License

CrossFrameMessenger is licensed under the MIT license. See LICENSE for more information.