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

peer-message

v0.3.0

Published

Fast peer to peer messaging through WebRTC with automatic reconnect.

Downloads

5

Readme

Peer Message

Fast peer to peer messaging through WebRTC with automatic reconnect.

Problem

Sending data between clients on the web is inherently slow due to two reasons.

  1. Data must travel from client -> server -> client. This means a longer round trip for your data and increased latency.

  2. Data is sent using TCP, this favoures reliability over speed as packets must be delivered in the correct order.

Solution

WebRTC is an open standard for sending information directly between clients. This technology supports video and voice but also arbitrary data. By utilising this we can acheive sub 80ms messaging between clients. Read more about WebRTC

Installation

npm i peer-message

Usage Example

import { PeerMessage } from 'peer-message';

const peerMessage = new PeerMessage(...args);

peerMessage.on('data', data => {
  // handle data
});

peerMessage.on('connect', () => {
  peerMessage.send({ hello: 'world });
});

peerMessage.host();

Documentation

PeerMessage

Create a new instance of PeerMessage which accepts signaling config and optional iceConfig.

const peerMessage = new PeerMessage({
  iceConfig: [],
  signal: {
    send: data => {
      websocket.send(data);
    },
    receive: update => {
      websocket.onmessage(data => {
        update(data);
      });
    },
  },
});

on

Listen for events.

Possible events are:

connect - Clients have successully established a connection
disconnect - The connection between two clients was lost or closed
data - Data was received from a remote client
error - An error has occured

peerMessage.on('data', () => {});

send

Send data to a remote client. Objects are stringified/parsed automatically.

peerMessage.send({ hello: 'world' });

host

Host a channel

peerMessage.host();

join

Connect to a host

peerMessage.join();

Signaling

Signaling is needed in order for two peers to share how they should connect. Usually this is solved through a regular HTTP-based Web API (i.e., a REST service or WebSockets) where web applications can relay the necessary information before the peer connection is initiated.

PeerMessage makes signaling easy. You simply need to relay the information given and received. Here is an example of signaling using websockets.

const start = () => {
  const peerMessage = new PeerMessage({
    iceConfig: [],
    signal: {
      send: data => {
        websocket.send(data);
      },
      receive: update => {
        websocket.onmessage(data => {
          update(data);
        });
      },
    },
  });
};

websocket.onconnected(() => {
  start();
});

IceConfig

Clients may not be able to connect directly if they are behind a NAT or when connecting over a mobile network such as 3/4G. You can read more detail on this here https://bloggeek.me/webrtc-turn/

We provide a default config pointing to free stun servers hosted by google. However you should not rely on this for production.

[
  { "urls": "stun:stun.l.google.com:19302" },
  { "urls": "stun:global.stun.twilio.com:3478?transport=udp" }
]

Twilio provides a great network traversal service which can be used by WebRTC, here is an example of the twilio config being used with PeerMessage. https://www.twilio.com/stun-turn

const peerMessage = new PeerMessage({
  iceConfig: [
    {
      url: 'stun:global.stun.twilio.com:3478?transport=udp',
      urls: 'stun:global.stun.twilio.com:3478?transport=udp',
    },
    {
      url: 'turn:global.turn.twilio.com:3478?transport=udp',
      username:
        '9e4b5cd9b97055a182295750fcf27000a51fd167d43061f379a49002bc9d5ef5',
      urls: 'turn:global.turn.twilio.com:3478?transport=udp',
      credential: 'jf308/4r9+uPbeEYn+ho918XDlVWVWcdtWJ/Bd7R1eP=',
    },
    {
      url: 'turn:global.turn.twilio.com:3478?transport=tcp',
      username:
        '9e4b5cd9b97055a182295750fcf27000a51fd167d43061f379a49002bc9d5ef5',
      urls: 'turn:global.turn.twilio.com:3478?transport=tcp',
      credential: 'jf308/4r9+uPbeEYn+ho918XDlVWVWcdtWJ/Bd7R1eP=',
    },
    {
      url: 'turn:global.turn.twilio.com:443?transport=tcp',
      username:
        '9e4b5cd9b97055a182295750fcf27000a51fd167d43061f379a49002bc9d5ef5',
      urls: 'turn:global.turn.twilio.com:443?transport=tcp',
      credential: 'jf308/4r9+uPbeEYn+ho918XDlVWVWcdtWJ/Bd7R1eP=',
    },
  ],
});

Test App

See our example test app for a complete example with websocket signaling

Test App