Git Product home page Git Product logo

kad's Introduction

Kad

Build Status Coverage Status NPM

Extensible implementation of the Kademlia distributed hash table for Node.js and the browser.

Quick Start

For complete documentation on using and extending Kad, read the documentation.

npm install [email protected]

Create your node, plug in your storage adapter, join the network, and party!

var kademlia = require('kad');

var seed = {
  address: '127.0.0.1',
  port: 1338
};

var dht = new kademlia.Node({
  transport: kademlia.transports.UDP(
    kademlia.contacts.AddressPortContact({
      address: '127.0.0.1',
      port: 1337
    })
  ),
  storage: kademlia.storage.FS('path/to/datadir')
});

dht.connect(seed, function(err) {
  // dht.get(key, callback);
  // dht.put(key, value, callback);
});

You can build Kad for the browser by running:

npm run build

This will output to dist/kad.browser.js and will bind to window when loaded in your web application.

You can run a network simulation locally using the included simulator. This will create n nodes (as you define) and connect them to each other, sending STORE messages on an interval and printing information to the console.

# use the default of 6 nodes
npm run simulation
# specify as many nodes as you like
npm run simulation 128

Transports

Kad ships with support for UDP, TCP, and HTTP transports. To explicitly define the transport to use, set the transport option to the appropriate value. See the documentation on kad.RPC and kad.Contact for more information.

var dht = new kademlia.Node({
  // ...
  transport: kademlia.transports.TCP(contact, options)
});

If you would like to author your own transport adapter, see kad-transport-boilerplate.

Community Transport Adapters

Persistence

Kad does not make assumptions about how your nodes will store their data, instead relying on you to implement a storage adapter of your choice. This is as simple as providing get(key, callback), put(key, value, callback), del(key, callback), and createReadStream() methods.

This works well with levelup, but you could conceivably implement any storage layer you like provided you expose the interface described above. Some adapters have already been contributed by the community, listed below.

Community Storage Adapters

NAT Traversal and Hole Punching

If your program runs on a user's personal computer, it's very likely that you will need to forward a port on their router so peers can communicate when behind a firewall. This is easy to do, using Indutny's node-nat-upnp module.

You'll want to do this before instantiating the Kademlia node.

var dht, nat = require('nat-upnp').createClient();

nat.portMapping({ public: 65535, private: 65535, ttl: 0 }, function(err) {
  nat.externalIp(function(err, ip) {
    dht = kad.Node(options);
  });
});

Projects Using Kad

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

kad's People

Contributors

gordonwritescode avatar omphalos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.