GithubHelp home page GithubHelp logo

alexxnica / discovery-swarm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mafintosh/discovery-swarm

0.0 0.0 0.0 60 KB

A network swarm that uses discovery-channel to find peers

License: MIT License

JavaScript 100.00%

discovery-swarm's Introduction

discovery-swarm

A network swarm that uses discovery-channel to find and connect to peers.

This module implements peer connection state and builds on discovery-channel which implements peer discovery. This uses TCP sockets by default and has experimental support for UTP.

npm install discovery-swarm

build status

Usage

var swarm = require('discovery-swarm')

var sw = swarm()

sw.listen(1000)
sw.join('ubuntu-14.04') // can be any id/name/hash

sw.on('connection', function (connection) {
  console.log('found + connected to peer')
})

API

var sw = swarm(opts)

Create a new swarm. Options include:

{
  id: crypto.randomBytes(32), // peer-id for user
  stream: stream, // stream to replicate across peers
  connect: fn, // connect local and remote streams yourself
  utp: true, // use utp for discovery
  tcp: true, // use tcp for discovery
  maxConnections: 0, // max number of connections.
  whitelist: [] // array of ip addresses to restrict connections to
}

For full list of opts take a look at discovery-channel

sw.join(key, [opts], [cb])

Join a channel specified by key (usually a name, hash or id, must be a Buffer or a string). After joining will immediately search for peers advertising this key, and re-announce on a timer.

If you pass opts.announce as a falsy value you don't announce your port (e.g. you will be in discover-only mode)

If you specify cb, it will be called when the first round of discovery has completed. But only on the first round.

sw.leave(key)

Leave the channel specified key

sw.connecting

Number of peers we are trying to connect to

sw.queued

Number of peers discovered but not connected to yet

sw.connected

Number of peers connected to

sw.on('connection', function(connection, info) { ... })

Emitted when you connect to another peer. Info is an object that contains info about the connection

{
  type: 'tcp', // the type, tcp or utp
  initiator: true, // whether we initiated the connection or someone else did
  channel: Buffer('...'), // the channel this connetion was initiated on. only set if initiator === true
  host: '127.0.0.1', // the remote address of the peer.
  port: 8080, // the remote port of the peer.
  id: Buffer('...') // the remote peer's peer-id.
}

sw.listen(port)

Listen on a specific port. Should be called before add

License

MIT

discovery-swarm's People

Contributors

blahah avatar ebukahills avatar garbados avatar joehand avatar juliangruber avatar mafintosh avatar maxogden avatar okdistribute avatar pfrazee 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.