Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mafintosh/peer-network
Create servers/clients that listen on names instead of ports and hostnames and are accessible over the internet
https://github.com/mafintosh/peer-network
Last synced: 8 days ago
JSON representation
Create servers/clients that listen on names instead of ports and hostnames and are accessible over the internet
- Host: GitHub
- URL: https://github.com/mafintosh/peer-network
- Owner: mafintosh
- License: mit
- Created: 2016-08-08T13:03:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-22T11:47:03.000Z (over 7 years ago)
- Last Synced: 2024-10-23T01:37:50.723Z (17 days ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 183
- Watchers: 8
- Forks: 18
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-peer-to-peer - peer-network
- awesome-dat - peer-network - create internet-accessible servers/clients listening on names, not hostnames (Dat Core Modules / Other Related Dat Project Modules)
- awesome-peer-to-peer - peer-network
README
# peer-network
Create servers/clients that listen on names instead of ports and hostnames and are accessible over the internet.
Uses [hyperdht](https://github.com/mafintosh/hyperdht) to discover peers and holepunch connections to them.Per default it uses `bootstrap1.hyperdht.org` to bootstrap the DHT but you can configure your own.
```
npm install peer-network
```[![build status](http://img.shields.io/travis/mafintosh/peer-network.svg?style=flat)](http://travis-ci.org/mafintosh/peer-network)
## Usage
First create a server
``` js
var peernet = require('peer-network')
var network = peernet()var server = network.createServer()
server.on('connection', function (stream) {
console.log('new connection')
stream.pipe(stream) // echo
})server.listen('echo-server') // listen on a name
```In another process (on any machine)
``` js
// will connect to a server annoucing itself as echo-server
var stream = network.connect('echo-server')stream.write('hello world')
stream.on('data', function (data) {
console.log('data:', data.toString())
})
```## API
#### `var network = peernet(opts)`
Create a new network instance. Options are forwarded to the [hyperdht](https://github.com/mafintosh/hyperdht) constructor.
If you do not provide a bootstrap list, `bootstrap1.hyperdht.org` is used.#### `var server = network.createServer([onconnection])`
Create a new server.
#### `server.listen(name, [onlistening])`
Listen on a name. Can be any buffer/string. Optionally you can specify a port to bound to as well. If not specified a random open port will be used.
The server will use discovery-channel to announce itself to other peers using multicast-dns, the bittorrent dht and potentially a series of dns servers.#### `server.close([onclose])`
Close the server and stop announcing its pressence
#### `server.on('connection', stream)`
Emitted when a client connects
#### `server.on('listening')`
Emitted when the server is listening.
#### `server.on('error', err)`
Emitted if the server has a critical error.
#### `server.on('close')`
Emitted when the server is fully close
#### `var stream = network.connect(name)`
Connect to a server listening on a name. If multiple servers are listening it will connect to the first one to which an connection can be established.
#### `stream.on('connect')`
Emitted when the stream is fully connected to another peer. You do not need to wait for this event before writing data to the socket.
## License
MIT