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

mplaynet

v0.0.5

Published

Mesh WebRTC library

Downloads

13

Readme

mplaynet

Typescript library to manage a mesh WebRTC network (beta)

Features

  • Manage multiple WebRTC connections
  • Provides multiple signaling mechanisms so you don't have to worry about the offer / answers / iceCandidates racking:
    • Signaling using Deepstream.io into a separate bundle
    • Signaling using firebase into a separate bundle
    • Signaling using localStorage for development purposes
  • Add header with timestamp and sequence to messages
  • Handle disconnections gracefully
  • Communication primitives
    • send(uuid: string, message: ArrayBuffer): boolean
    • sendAndListen(uuid: string, message: ArrayBuffer): Promise
    • reply(uuid: string, originalMessage: Message, message: ArrayBuffer): boolean
    • replyAndListen(uuid: string, originalMessage: Message, message: ArrayBuffer): Promise
    • broadcast(message: ArrayBuffer)
    • broadcastAndListen(message: ArrayBuffer): Promise[]
  • Calculate latency between peers at regular intervals
  • Clock synchronization by Cristian's algorithm

Install and usage

You can either import mplaynet via NPM or directly use it via script tag.

NPM:

First, run: npm i mplaynet

import { Mesh } from 'mplaynet';

const myMesh = new Mesh();

Directly in the browser

Add this script tag:

<script src="https://unpkg.com/mplaynet@latest/dist/mplaynet.umd.min.js"></script>

For Deepstream signaling add these tags:

<script src="//cdn.jsdelivr.net/npm/@deepstream/[email protected]/dist/bundle/ds.min.js"></script>
<script src="https://unpkg.com/mplaynet@latest/dist/mplaynet-deepstream.umd.min.js"></script>

For Firebase signaling add these tags:

<script src="https://www.gstatic.com/firebasejs/8.2.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.3/firebase-firestore.js"></script>
<script src="https://unpkg.com/mplaynet@latest/dist/mplaynet-firebase.umd.min.js"></script>

Establish connections, send and receive messages

see demo folder for details

const meshConfig = new MeshConfig(...);
const mesh = new Mesh(meshConfig, myUUID);

// deepstream signaling
const { DeepstreamSignaling } = mplaynetDeepstream;
const signaller = new DeepstreamSignaling(DEEPSTREAM_URL);
// OR firebase signaling
const { FirebaseSignaling } = mplaynetFirebase;
const signaller = new FirebaseSignaling({
  apiKey: FIREBASE_API_KEY,
  authDomain: FIREBASE_AUTH_DOMAIN,
  projectId: FIREBASE_PROJECT_ID
});
// OR localStorage signaling
const { LocalSignaling } = mplaynet;
const signaller = new LocalSignaling();

// create a new room...
signaller.hostRoom(roomId, username, myUUID);
// ...or join an existin room
signaller.joinRoom(roomId, username, myUUID)
 // Triggered when a player joins the room or when he is ready to play.
signaller.roomRecordEmitter.addEventListener((uuid, event) => { 
  // when all players are ready, start pairing:
  if (....all players ready...)
    signaller.startPairings(mesh).then((ok) => {
      if (ok) {
        // start game
      }
    });
});

// broadcast a message to all peers
const message = new ArrayBuffer(size);
mesh.broadcast(message);

// broadcast a message to all peers an listen for replies
const greeting = new TextEncoder().encode('hello all!, I am Peter!').buffer;
mesh.broadcastAndListen(greeting).forEach(promise => promise.then(reply => {
  console.log(new TextDecoder().decode(reply.body));
}));

// send a message to a peer
mesh.send(remotePeer.uuid, message);

// send a message to a peer and listen for reply
const greeting = new TextEncoder().encode('hello, I am Peter!').buffer;
mesh.sendAndListen(remotePeer.uuid, greeting).then(reply => {
  console.log(new TextDecoder().decode(reply.body));
});

// receive messages from remote peers
mesh.messageEmitter.addEventListener((uuid, message) => {
  // uuid of the remote peer
  // message.timestamp : remote timestamp
  // message.timestampToLocalTime : remote timestamp converted to local time
  // message.sequence: message sequence
  // message.body: ArrayBuffer
  // message.type (1 - send ; 2 - sendAndListen ; 3 - reply ; 4 - replyAndListen)
  // i.e. info = new Int16Array(message.body);

  // if the message was sent via 'sendAndListen' or 'replyAndListen',
  // the peer is waiting for a reply
  if (message.awaitReply) {
      const response = new TextEncoder().encode('nice to meet you').buffer;
      // send the reply
      mesh.reply(uuid, message, response);
      // OR send the reply and wait for a counter reply
      mesh.replyAndListen(uuid, message, response).then(counterReply => {
        console.log(new TextDecoder().decode(counterReply.body));
      });
      return;
  }
});

// disconnection management
mesh.connectionReadyEmitter.addEventListener((uuid, ready) => {
    if (!ready) { // player disconnected
      // ...remove player from screen
    }
});

Demos