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

meshp2p

v1.0.30

Published

https://github.com/HadiModarres/MeshP2P

Downloads

24

Readme

MeshP2P

Create P2P apps between browsers

MeshP2P is a framework that allows bootstrapping the development of a distributed application over web browsers using WebRTC. It allows for discovery of other peers without requiring a central server. This is done through regular exchange of information between peers.

Usage

npm install meshp2p --save

Importing in your js code

const Node = require("meshp2p").Node;

API

Node Constructor

As a minimum a callback for incoming rtc connections, and an array of signalling servers should be provided to the Node constructor.

Arguments
  1. InboundCb (func): a function that is called when another peer makes a connection to this node. The rtcDataChannel will provided as an argument to the callback function.
  2. Options (Obj): parameters, DEFAULT_SIGNALLING_SERVERS is required
Example

Assuming two signalling servers are running locally on ports 12345 and 12346:

   node = new Node((rtcDataChannel)=>{
            // do something with inbound connection
        },{DEFAULT_SIGNALLING_SERVERS:[
                {
                    "socket": {
                        "server": "http://127.0.0.1:12345"
                    },
                    "signallingApiBase": "http://127.0.0.1:12345"
                },
                {
                    "socket": {
                        "server": "http://127.0.0.1:12346"
                    },
                    "signallingApiBase": "http://127.0.0.1:12346"
                }
            ]});

See Signalling Servers on how to start signalling servers.

registerList

MeshP2P allows for multiple lists to exist in the network, and each node is able to register multiple entries in each list. Make a global list in the network using registerList.

Arguments
  1. list (string): The name of the list
  2. proximityFunction (func): The proximity function specifies how closeness between nodes is defined. It has the form (entry1, entry2) => float. The higher returned score from proximity function means the two entries are more identical, and a score of 0 means least identical.
  3. responseMinScore (float): this is the minimum closeness score between entries to consider it a hit, and a node would respond to the query.
Returns

Void.

Example 1

Consider a network with a list of names of peers. In this case it's natural to consider closeness between entries to be string similarity:

registerList("list#name", (str1, str2) => {return stringSimilarity(str1,str2)}, 0.7)
Example 2

Consider a network of peers with each peer having a coordinate in 2D space. In this case one can define the closeness to be inverse of their euclidean distance, and a distance of less than 4 to consider a hit:

registerList("list#coordinates", (entry1,entry2) => {return 1/euclideanDist(entry1,entry2)}, 1/4)

setEntries

Set the entries for the current node in the specified network list.

Arguments
  1. list (string): The global list
  2. entries (object[]): the entries for this node
Returns

Void.

Examples
setEntries("list#names", ["Jack"])
setEntries("list#coordinates",[{x: 3,y:12}])

Search

Search the network.

Arguments
  1. list(string): The global list to search
  2. query(obj): The query. The query should have the form of list entry, and will be fed to the provided proximityFunction provided.
  3. timeout(int): Number of seconds to wait for responses from the network. After that, the resources are freed and responses for this query aren't handled.
  4. searchResultCallback(func): The callback is called each time a response is received from the network. The response is passed to the callback function and has the form: {key,value}. key is the entry that caused a match, and the value is the nodePointer of the peer that has responded to the query.
Examples
search("list#names", "jacky", 60, (response)=>{
// do something with the response
});
search("list#coordinates", {x:2,y:2}, 60, (response)=>{
// do something with the response
});

connectToNode

Arguments

nodePointer(nodePointerObj): The node pointer of the peer to connect to.

Returns

A promise that is resolved with an rtcDataChannel (https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel) when successfully connected to the target peer.

Examples
node.search("list#names", "jacky", 60, (response)=>{
   node.connectToNode(response.value).then(rtcDataChannel)=>{
       rtcDataChannel.send("hello"); }) 
});

startNode

Starts the node. Start node only after specifying the global list and the node's entries in the list.

Arguments

None

Signalling Servers

WebRTC requires the use of signalling servers so that peers can negotiate for a connection. Signalling servers are provided as part of MeshP2P and can be easily started using:

npm run server -- 12345

This runs a signalling server on port:12345

Peers in the network should have access to at least one signalling server, and this should be specified in the node constructor when creating peers.