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

smart-postmessage

v1.0.11

Published

Easy to communicate with iframe.Use request, subscribe, observe and notify to make postmessage simple and easy.

Downloads

96

Readme

Smart Postmesage

smart postmessage is a tool to help you make a connection with iframe through postmessage.

Simplified Communication

  • 🍜 Allowing users to use requests as promises makes it more user-friendly.
  • 📡 Establishing secure communication channels between parent and child windows, support to set timeout and retry gap.
  • 📮 Request and Respond for Method Invocation
  • 📢 Subscribing to and notify events.
  • ✨ Clean and easy-to-use API.

Installation

via npm

  npm i smart-postmessage

via yarn

  yarn add smart-postmessage

Usage

Initiate

import SmartPostMessage, { MethodMap } from 'smart-post-message';

// Define the method map for observed and requested methods
interface ObservedMethods extends MethodMap {
  getData: (data: number) => number;
  userLoggedIn: () => boolean;
}

// Define the method map for notify and subscribe methods
interface NotifyMethods extends MethodMap {
  somebodyJoined: () => { userId: string };
}

// Create an instance of SmartPostMessage
// You can configure communication timeouts and retries
const smartPostMessage = new SmartPostMessage<NotifyMethods, ObservedMethods>({
  currentWindow: window,
  targetWindow: targetWindow,
  targetOrigin: 'https://example.com',
  establishTimeout: 30000,
  establishInterval: 3000,
});

// Establish a communication channel
await smartPostMessage.establish(true).then(() => {
}).catch(() => {
  // Communication failed
});

// Communication is established
// You can now send and receive messages
// ...

Requester and Responder

Initiates communication with smartPostMessage.request().

// Trigger the 'userLoggedIn' event in the server window
await smartPostMessage.request('userLoggedIn'); // return boolean.

Listens and responds with smartPostMessage.observe().

// Observe an event (e.g., 'userLoggedIn')
smartPostMessage.observe('userLoggedIn', () => {
  // Handle the event (e.g., perform some action when a user logs in)
  return true;
});

Notifying and Subscribing to Events

You can use the "notify" and "subscribe" pair for event notification:

Broadcast events using smartPostMessage.notify().

// In the notifier window
smartPostMessage.notify('somebodyJoined', { userId: 'userA' });

Listen and react to events with smartPostMessage.subscribe().

// In the subscriber window
const unsubscribe = smartPostMessage.subscribe('somebodyJoined', (data) => {
  // Handle the event data
  console.log(data);
  // { userId: 'userA' }
});

// Don't forget to unsubscribe when no longer needed
unsubscribe();

Finish the communication

smartPostMessage.finish();

License

MIT