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

@jurca/post-message-p2p

v1.1.3

Published

P2P reliable and semi-secure messaging between contexts using the postMessage API.

Downloads

10

Readme

post-message-p2p

Build Status npm License npm type definitions

A client-server or peer-to-peer postMessage-based messaging library with TypeScript support.

Installation

post-message-p2p is available as npm package, you can use npm to install it:

npm install --save @jurca/post-message-p2p

Usage

Client-server messaging model

To connect to another context (frame, window, service worker or another object implementing the postMessage API), use the connect function:

import {connect} from '@jurca/post-message-p2p'

(async () => {
  const sendMessage = await connect(someIframe.contentWindow, {
    // A string, number, symbol, boolean, null or undefined identifying the
    // communication channel with the peer.
    channel: 'foo',
    // An optional timeout for receiving a confirmation that the peer has
    // received the message, defaults to 10 seconds. Specified in milliseconds,
    // must be a positive integer.
    timeout: 100,
    // The optional origin that is allowed to receive messages sent through
    // this connection. Defaults to '*', but is recommended to be set for
    // security reasons.
    origin: 'https://some.origin.org',
    // The optional number of retries when trying to perform a handshake with
    // the provided peer. The connection will not be established if the peer
    // will not be responding to the handshake messages. Defaults to 2.
    handshakeRetries: 2,
    // An optional delay between handshake attempts in milliseconds. Defaults
    // to 500.
    handshakeRetryDelay: 3000,
  })

  // The returned promise resolves when the peer confirms receiving the
  // message, or rejects if the peer does not respond within the timeout.
  await sendMessage(anyDataYouWantToSend)
})()

Note that the peer must be listening for messages for a connection to be established. Use the listen function to listen for incoming messages:

import {listen} from '@jurca/post-message-p2p'

listen(
  // A string, number, symbol, boolean, null or undefined identifying the
  // communication channel with the peer.
  'foo',
  // Whitelisted origins from which the messages will be received. Messages
  // originating in other origins will be ignored. Use an empty array if you
  // need to listen for messages from any origin, however, this is highly
  // discouraged for security reasons.
  ['https://some.origin.org', 'https://other.origin.org'],
  (messageData) => {
    // This callback will be invoked for every data message sent to this peer
    // from a whitelisted origin if matching the specified channel.
    // The listener will confirm receiving the message to the sender *after*
    // invoking this callback. No confirmation will be sent to the sender if
    // this callback throws an error. The value returned from the callback
    // is ignored.
  },
)

Peer-to-peer messaging model

Use the createAgent to establish a connection with a peer:

import {createAgent} from '@jurca/post-message-p2p'

(async () => {
  const sendMessage = await createAgent({
    peer: someIframe.contentWindow,
    channel: 'fooBar',
    onMessage: (data) => {
      // The callback invoked for every data message (see the listen function).
    },
    // The rest of optional configuration options passed to the connect
    // function is allowed here too.
  })
})()

The createAgent will set up a message listener for incoming messages (just like the listen function) and then attempts to create a connection and do a handshake with the provided peer (just like the connect function).