├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - '0.12' 5 | - 'iojs' 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # airswarm 2 | 3 | Network swarm that automagically discovers other peers on the network using multicast dns 4 | 5 | ``` 6 | npm install airswarm 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/airswarm.svg?style=flat)](http://travis-ci.org/mafintosh/airswarm) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var airswarm = require('airswarm') 15 | 16 | airswarm('testing', function (sock) { 17 | sock.write('hello world (' + process.pid + ')\n') 18 | sock.pipe(process.stdout) 19 | }) 20 | ``` 21 | 22 | If you run the above program in a couple of processes on the same local network 23 | the swarms should start connecting to each other and write hello world 24 | 25 | ## API 26 | 27 | #### `swarm = airswarm(name, [options], [onpeer])` 28 | 29 | Create a new swarm. The `swarm` will emit `peer` everytime a new peer 30 | is connected. Optionally you can pass a `peer` listener as the second argument. 31 | 32 | The `peer` will be a tcp stream to another swarm. 33 | 34 | Options include 35 | 36 | ``` js 37 | { 38 | limit: maxPeersToConnectTo // defaults to Infinity 39 | } 40 | ``` 41 | 42 | #### `swarm.peers` 43 | 44 | An array containing all the currently connected peers 45 | 46 | ## License 47 | 48 | MIT 49 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var airswarm = require('airswarm') 2 | 3 | airswarm('testing', function (sock) { 4 | sock.write('hello world (' + process.pid + ')\n') 5 | sock.pipe(process.stdout) 6 | }) 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var multicastdns = require('multicast-dns') 2 | var net = require('net') 3 | var addr = require('network-address') 4 | 5 | module.exports = function airswarm (name, opts, fn) { 6 | if (typeof opts === 'function') return airswarm(name, null, opts) 7 | if (!opts) opts = {} 8 | 9 | var limit = opts.limit || Infinity 10 | var mdns = multicastdns() 11 | var connections = {} 12 | 13 | var server = net.createServer(function (sock) { 14 | sock.on('error', function (err) { 15 | sock.destroy(err) 16 | }) 17 | track(sock) 18 | }) 19 | 20 | server.peers = [] 21 | 22 | function track (sock) { 23 | if (server.peers.length >= limit) return sock.destroy() 24 | server.peers.push(sock) 25 | sock.on('close', function () { 26 | server.peers.splice(server.peers.indexOf(sock), 1) 27 | }) 28 | server.emit('peer', sock) 29 | } 30 | 31 | server.on('listening', function () { 32 | var host = addr() 33 | var port = server.address().port 34 | var id = host + ':' + port 35 | 36 | mdns.on('query', function (q) { 37 | for (var i = 0; i < q.questions.length; i++) { 38 | var qs = q.questions[i] 39 | if (qs.name === name && qs.type === 'SRV') return respond() 40 | } 41 | }) 42 | 43 | mdns.on('response', function (r) { 44 | for (var i = 0; i < r.answers.length; i++) { 45 | var a = r.answers[i] 46 | if (a.name === name && a.type === 'SRV') connect(a.data.target, a.data.port) 47 | } 48 | }) 49 | 50 | update() 51 | var interval = setInterval(update, 3000) 52 | 53 | server.on('close', function () { 54 | clearInterval(interval) 55 | }) 56 | 57 | function respond () { 58 | mdns.response([{ 59 | name: name, 60 | type: 'SRV', 61 | data: { 62 | port: port, 63 | weigth: 0, 64 | priority: 10, 65 | target: host 66 | } 67 | }]) 68 | } 69 | 70 | function update () { 71 | if (server.peers.length < limit) mdns.query([{name: name, type: 'SRV'}]) 72 | } 73 | 74 | function connect (host, port) { 75 | var remoteId = host + ':' + port 76 | if (remoteId === id) return 77 | if (connections[remoteId]) return 78 | if (remoteId < id) return respond() 79 | 80 | var sock = connections[remoteId] = net.connect(port, host) 81 | 82 | sock.on('error', function () { 83 | sock.destroy() 84 | }) 85 | 86 | sock.on('close', function () { 87 | delete connections[remoteId] 88 | }) 89 | 90 | track(sock) 91 | } 92 | }) 93 | 94 | if (fn) server.on('peer', fn) 95 | server.listen(0) 96 | 97 | return server 98 | } 99 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "airswarm", 3 | "version": "1.1.0", 4 | "description": "Network swarm that automagically discovers other peers on the network using multicast dns", 5 | "main": "index.js", 6 | "dependencies": { 7 | "multicast-dns": "^3.0.0", 8 | "network-address": "^1.0.0" 9 | }, 10 | "devDependencies": { 11 | "standard": "^5.2.1", 12 | "tape": "^4.2.0" 13 | }, 14 | "scripts": { 15 | "test": "standard && tape test.js" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/mafintosh/airswarm.git" 20 | }, 21 | "author": "Mathias Buus (@mafintosh)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/mafintosh/airswarm/issues" 25 | }, 26 | "homepage": "https://github.com/mafintosh/airswarm" 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var airswarm = require('./') 3 | 4 | tape('connects', function (t) { 5 | t.plan(3) 6 | 7 | var once1 = true 8 | var once2 = true 9 | 10 | airswarm(process.pid + '-testing--', function (sock) { 11 | t.ok(once1, 'got socket') 12 | once1 = false 13 | sock.on('data', function (data) { 14 | t.same(data.toString(), '+') 15 | }) 16 | }) 17 | 18 | airswarm(process.pid + '-testing--', function (sock) { 19 | t.ok(once2, 'got socket') 20 | once2 = false 21 | sock.write('+') 22 | }) 23 | }) 24 | 25 | tape('exits', function (t) { 26 | t.end() 27 | process.exit(0) 28 | }) 29 | --------------------------------------------------------------------------------