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

p2talk

v1.0.1

Published

Decentralized Internet p2p module [GSM Alternative]

Downloads

6

Readme

This package is under construction and not yet in a production ready environment

p2talk

p2talk is an implementation of the original old school p2p protocol with the decentralized internet as an optional sync host for the backend


p2p

p2p implements a peer-to-peer protocol.

Installation

$ npm install p2p

Quick start

First you need to add a reference to p2p.

const p2p = require('p2p');

Then create a new peer by calling the peer function and specifying the host and the port to listen on.

Additionally, you need to specify a private key and a certificate. Please note that these values must be strings that contain data in .pem format.

const peer = p2p.peer({
  host: 'localhost',
  port: 3000,
  privateKey: '...',
  certificate: '...'
});

If you do not want to use encryption you may omit the private key and the certificate. Then, the network will use plain text messages. Please note that this should be avoided since it is not secure!

const peer = p2p.peer({
  host: 'localhost',
  port: 3000
});

Optionally you may specify a metadata property to attach arbitrary data to a peer. These metadata will be available to others when asking for information about the peer. You may use it, e.g., to store information on services a peer offers.

const peer = p2p.peer({
  host: 'localhost',
  port: 3000,
  privateKey: '...',
  certificate: '...',
  metadata: {
    foo: 'bar'
  }
});

Joining a network

To join a network, provide the host and the port of another peer that you want to join using the wellKnownPeers property.

const peer = p2p.peer({
  host: 'localhost',
  port: 3000,
  privateKey: '...',
  certificate: '...',
  wellKnownPeers: { host: 'localhost', port: 4000 }
});

You may also specify multiple peers. This increases the chance to join a network even in case that some peers are down.

const peer = p2p.peer({
  host: 'localhost',
  port: 3000,
  privateKey: '...',
  certificate: '...',
  wellKnownPeers: [
    { host: 'localhost', port: 4000 },
    { host: 'localhost', port: 5000 },
    { host: 'localhost', port: 6000 }
  ]
});

If the peer leaves the network, e.g. because of a connection error, it automatically tries to rejoin. For that it manages an internal list of peers that it got to know. To retrieve this list run the wellKnownPeers.get function.

const wellKnownPeers = peer.wellKnownPeers.get();

You may store this list and re-use it when setting up a peer from scratch for the next time.

To get the status of a peer call its status function.

console.log(peer.status());
// => 'lonely' or 'unbalanced' or 'joined'

Additionally, you may subscribe to the status::* event to get notified whenever the status of a peer changes.

peer.on('status::*', status => {
  console.log(status);
  // => {
  //      from: 'lonely',
  //      to: 'unbalanced'
  //    }
});

If you are interested in entering a specific status, you may also subscribe to the more specialized events status::lonely, status::unbalanced and status::joined.

Configuring housekeeping

By default, a peer tries to do housekeeping around every 30 seconds. If you need to change this, provide a property called serviceInterval.

const peer = p2p.peer({
  host: 'localhost',
  port: 3000,
  privateKey: '...',
  certificate: '...',
  wellKnownPeers: [ ... ],
  serviceInterval: '10s'
});

Please note that this affects the way the protocol works. Hence setting the serviceInterval property should be avoided in most cases.

Finding the responsible peer

If you want to find the peer responsible for a value, call the getEndpointFor function and provide the value as a string.

As a result you will get the endpoint of the peer as well as its metadata. If no metadata have been set, an empty object is returned.

peer.getEndpointFor('foobar', (err, endpoint, metadata) => {
  // ...
});

If you want to know whether a peer is responsible for a specific value, call the isResponsibleFor and provide the value in question.

console.log(peer.isResponsibleFor('foobar'));
// => true

Detecting changes in your neighborhood

To detect whether the successor or predecessor of a peer changed, subscribe to the environment::successor and environment::predecessor events. Please note that the predecessor may be undefined.

peer.on('environment::successor', successor => {
  // ...
});

peer.on('environment::predecessor', predecessor => {
  // ...
});

Please note that you can also subscribe to any environmental changes using a wildcard.

peer.on('environment::*', successorOrPredecessor => {
  // ...
});

Registering actions

To register custom actions, add them to the handle object. If an action is called on a peer, the module will call the appropriate function automatically. Once you are done you need to call the done callback. Optionally, you may provide a result.

peer.handle.foo = (payload, done) => {
  // Do something with payload...
  if (err) {
    return done(err);
  }
  done(null);
  // or: done(null, result);
};

Calling actions on remote peers

If you want to call an action on a remote peer, call the remote.run function, and provide the name of the action prefixed with handle/ as well as its arguments and a callback. If the action returns a result, the callback has a result parameter.

peer.remote({
  host: 'localhost',
  port: 4000
}).run('handle/foo', { foo: 'bar' }, (err, result) => {
  // ...
});

Running the build

This module can be built using Grunt. Besides running the tests, this also analyses the code. To run Grunt, go to the folder where you have installed p2p and run grunt. You need to have grunt-cli installed.

$ grunt

To run the integration tests setup Docker and run Grunt using the integration command.

$ grunt integration

License

The MIT License (MIT) Copyright (c) 2012-2015 the native web.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.