{"id":13482991,"url":"https://github.com/mafintosh/discovery-swarm","last_synced_at":"2025-04-04T07:08:09.152Z","repository":{"id":57212848,"uuid":"51326935","full_name":"mafintosh/discovery-swarm","owner":"mafintosh","description":"A network swarm that uses discovery-channel to find peers","archived":false,"fork":false,"pushed_at":"2020-08-02T20:40:17.000Z","size":111,"stargazers_count":377,"open_issues_count":29,"forks_count":50,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-03-28T06:08:16.756Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mafintosh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-08T21:04:04.000Z","updated_at":"2025-02-15T17:04:31.000Z","dependencies_parsed_at":"2022-09-10T22:50:54.388Z","dependency_job_id":null,"html_url":"https://github.com/mafintosh/discovery-swarm","commit_stats":null,"previous_names":[],"tags_count":52,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-swarm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-swarm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-swarm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fdiscovery-swarm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mafintosh","download_url":"https://codeload.github.com/mafintosh/discovery-swarm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247135144,"owners_count":20889421,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:07.289Z","updated_at":"2025-04-04T07:08:09.096Z","avatar_url":"https://github.com/mafintosh.png","language":"JavaScript","funding_links":[],"categories":["Dat Core Modules","JavaScript","P2P","Modules"],"sub_categories":["Networking","React Components"],"readme":"# discovery-swarm\n\nA network swarm that uses [discovery-channel](https://github.com/maxogden/discovery-channel) to find and connect to peers.\n\nThis 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.\n\n```\nnpm install discovery-swarm\n```\n\n[![build status](http://img.shields.io/travis/mafintosh/discovery-swarm.svg?style=flat)](http://travis-ci.org/mafintosh/discovery-swarm)\n\n## Usage\n\n``` js\nvar swarm = require('discovery-swarm')\n\nvar sw = swarm()\n\nsw.listen(1000)\nsw.join('ubuntu-14.04') // can be any id/name/hash\n\nsw.on('connection', function (connection) {\n  console.log('found + connected to peer')\n})\n```\n\n## API\n\n#### `var sw = swarm(opts)`\n\nCreate a new swarm. Options include:\n```js\n{\n  id: crypto.randomBytes(32), // peer-id for user\n  stream: stream, // stream to replicate across peers\n  connect: fn, // connect local and remote streams yourself\n  utp: true, // use utp for discovery\n  tcp: true, // use tcp for discovery\n  maxConnections: 0, // max number of connections.\n  whitelist: [], // array of ip addresses to restrict connections to\n  keepExistingConnections: false  // by default, prefer tcp by dropping old utp connections\n}\n```\n\nFor full list of `opts` take a look at [discovery-channel](https://github.com/maxogden/discovery-channel)\n\n#### `sw.join(key, [opts], [cb])`\n\nJoin 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.\n\nIf you pass `opts.announce` as a falsy value you don't announce your port (e.g. you will be in discover-only mode)\n\nIf you specify cb, it will be called *when the first round* of discovery has completed. But only on the first round.\n\n#### `sw.leave(key)`\n\nLeave the channel specified `key`\n\n#### `sw.connecting`\n\nNumber of peers we are trying to connect to\n\n#### `sw.queued`\n\nNumber of peers discovered but not connected to yet\n\n#### `sw.connected`\n\nNumber of peers connected to\n\n#### `sw.on('peer', function(peer) { ... })`\n\nEmitted when a peer has been discovered. Peer is an object that contains info about the peer.\n\n```js\n{\n  channel: Buffer('...'), // the channel this connection was initiated on.\n  host: '127.0.0.1', // the remote address of the peer.\n  port: 8080, // the remote port of the peer.\n  id: '192.168.0.5:64374@74657374', // the remote peer's peer-id.\n  retries: 0 // the number of times tried to connect to this peer.\n}\n```\n\n\n#### `sw.on('peer-banned', function(peerAddress, details) { ... })`\n\nEmitted when a peer has been banned as a connection candidate. peerAddress is an object that contains info about the peer. Details is an object that describes the rejection.\n\n```js\n{\n  reason: 'detected-self' // may be 'application' (removePeer() was called) or 'detected-self'\n}\n```\n\n#### `sw.on('peer-rejected', function(peerAddress, details) { ... })`\n\nEmitted when a peer has been rejected as a connection candidate. peerAddress is an object that contains info about the peer. Details is an object that describes the rejection\n\n```js\n{\n  reason: 'duplicate' // may be 'duplicate', 'banned', or 'whitelist'\n}\n```\n\n#### `sw.on('drop', function(peer) { ... })`\n\nEmitted when a peer has been dropped from tracking, typically because it failed too many times to connect. Peer is an object that contains info about the peer.\n\n#### `sw.on('connecting', function(peer) { ... })`\n\nEmitted when a connection is being attempted. Peer is an object that contains info about the peer.\n\n#### `sw.on('connect-failed', function(peer, details) { ... })`\n\nEmitted when a connection attempt fails. Peer is an object that contains info about the peer. Details is an object that describes the failure\n\n```js\n{\n  timedout: true // was the failure a timeout?\n}\n```\n\n#### `sw.on('handshaking', function(connection, info) { ... })`\n\nEmitted when you've connected to a peer and are now initializing the connection's session. Info is an object that contains info about the connection.\n\n``` js\n{\n  type: 'tcp', // the type, tcp or utp.\n  initiator: true, // whether we initiated the connection or someone else did.\n  channel: Buffer('...'), // the channel this connection was initiated on. only set if initiator === true.\n  host: '127.0.0.1', // the remote address of the peer.\n  port: 8080, // the remote port of the peer.\n  id: Buffer('...') // the remote peer's peer-id.\n}\n```\n\n#### `sw.on('handshake-timeout', function(connection, info) { ... })`\n\nEmitted when the handshake fails to complete in a timely fashion. Info is an object that contains info about the connection.\n\n#### `sw.on('connection', function(connection, info) { ... })`\n\nEmitted when you have fully connected to another peer. Info is an object that contains info about the connection.\n\n#### `sw.on('connection-closed', function(connection, info) { ... })`\n\nEmitted when you've disconnected from a peer. Info is an object that contains info about the connection.\n\n#### `sw.on('redundant-connection', function(connection, info) { ... })`\n\nEmitted when multiple connections are detected with a peer, and so one is going to be dropped (the `connection` given). Info is an object that contains info about the connection.\n\n#### `sw.listen(port)`\n\nListen on a specific port. Should be called before join\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fdiscovery-swarm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmafintosh%2Fdiscovery-swarm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fdiscovery-swarm/lists"}