p2p

0.10.2 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i p2p

Weekly Downloads

15

Version

0.10.2

License

MIT

Last publish

Collaborators

  • thenativeweb-admin
  • goloroden