• Stars
    star
    152
  • Rank 244,685 (Top 5 %)
  • Language
    JavaScript
  • Created almost 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

👩↔️👨 Decentralized peer discovery and signaling

peer-exchange

npm version Build Status Dependency Status

Decentralized peer discovery and signaling

peer-exchange is a client for the Peer Exchange Protocol (PXP), a decentralized protocol for peer discovery and signaling. Rather than using centralized signal hubs, each node in the network exchanges peers and relays signaling data.

This client uses WebRTC for peer connections, but you may also use any other transport by manually connecting and passing in a duplex stream.

Usage

npm install peer-exchange

var Exchange = require('peer-exchange')

var ex = new Exchange('some-network-id', { wrtc: wrtc })
// The network id can be any string unique to your network.
// When using Node.js, the `wrtc` option is required.
// (This can come from the 'wrtc' or 'electron-webrtc' packages).

ex.on('connect', (conn) => {
  // `conn` is a duplex stream multiplexed through the PXP connection,
  // which can be used for your P2P protocol.
  conn.pipe(something).pipe(conn)

  // We can query our current peers for a new peer by calling `getNewPeer()`.
  // `peer-exchange` will do the WebRTC signaling and connect to the peer.
  if (ex.peers.length < 8) ex.getNewPeer()
})

// Bootstrap by connecting to one or more already-known PXP peers.
// You can use any transport that creates a duplex stream (in this case TCP).
var socket = net.connect(8000, '10.0.0.1', () => ex.connect(socket))

// You can optionally accept incoming connections using any transport.
var server = net.createServer((socket) => ex.accept(socket))
server.listen(8000)

API

Exchange

var Exchange = require('peer-exchange')

Methods

var ex = new Exchange(networkId, [opts])

Creates a new exchange, which is used to manage connections to peers in a P2P network. After we establish some initial connections, we can query our current peers for new peers. Additionally, we will share peers when we receive queries.

networkId should be a string unique to the network. Nodes can only peer with other nodes that use the same ID. If you need to participate in multiple networks, create multiple Exchange instances with different IDs.

opts should contain the following properties:

  • wrtc, Object - A WebRTC implementation for Node.js clients (e.g. wrtc or electron-webrtc). In browsers, the built-in implementation is used by default.

ex.connect(socket, [callback])

Manually adds a peer. This is necessary to bootstrap our exchange with initial peers which we can query for additional peers. This method is for outgoing connections, for incoming connections use accept.

socket should be a duplex stream that represents a connection to a peer which implements the Peer Exchange Protocol.

callback will be called with callback(err, connection), where connection is a duplex stream which may be used by your application for your P2P protocol.


ex.accept(socket, [callback])

Similar to connect, but used with incoming peer connections.


ex.getNewPeer([callback])

Queries out current peers for a new peer, then connects to it via WebRTC. The already-connected peer will act as a relay for signaling.

The 'connect' event will be emitted once the connection is established.

This will error if our exchange is not connected to any peers.

callback will be called with callback(err, connection).


ex.close([callback])

Closes all peer connections in the exchange and prevents adding any new connections.

callback is called with callback(err) when the exchange is closed (or when an error occurs).


Properties

ex.peers

An array of connected peers. Useful for iterating through peers or getting the number of connections.

ex.networkId

The network ID provided in the constructor.


Events

ex.on('connect', function (conn) { ... })

Emitted whenever a new peer connection is established (both incoming and outgoing).

ex.on('error', function (err) { ... })

Emitted when an error occurs.


Security Notes

Some efforts were made to make this module DoS-resistant, but there are probably still some weaknesses.

It is recommended to use an authenticated transport when possible (e.g. WebSockets over HTTPS) for initial bootstrapping to prevent man-in-the-middle attacks (attackers could control all the peers you connect to, which can be very bad in some applications).

Comparison with signalhub

This module provides functionality similar to signalhub, where P2P nodes can get addresses of new peers and establish connections by relaying signaling data. However, this module differs by getting all nodes to provide this "hub" service, rather than a few centralized servers. It also only exchanges currently-connected peer addresses rather than providing general-purpose broadcasting.

Note that signalhub may be better suited for some applications, for instance when connecting to peers in small swarms when no peer addresses are initially known (e.g. BitTorrent swarms). In the future, a DHT could help with finding initial peers for this sort of use case.

More Repositories

1

gitbanner

🎈 Generates a git repo to show a cool banner on your Github profile
JavaScript
596
star
2

webcoin

🌐💰 SPV Bitcoin client for Node.js and the browser
JavaScript
412
star
3

electron-webrtc

⚛ Use WebRTC in Node.js via a hidden Electron process
JavaScript
316
star
4

mercury

💱 Decentralized cryptocurrency exchange
Java
159
star
5

node-hackrf

📻 Control a HackRF device (e.g. Jawbreaker, HackRF One, or Rad1o) from Node.js
C
146
star
6

watt

💡 Powerful control flow using ES6 generators
JavaScript
142
star
7

DCPU-16

💾 A javascript emulator for DCPU-16 (the computer system in Mojang's new game, 0x10c). Works in browsers and Node.
JavaScript
92
star
8

rust-bitcoin-script

Inline Bitcoin scripts in Rust
Rust
86
star
9

js-tendermint

A JS light client for Tendermint blockchains
JavaScript
72
star
10

coins

Cryptocurrency middleware for Lotion
JavaScript
64
star
11

redstone

🔻 A distributed, infinite-player Minecraft server that runs on Node.js.
CoffeeScript
61
star
12

hackrf-stream

A stream interface to receive and transmit on a HackRF radio from Node.js
JavaScript
54
star
13

electron-eval

Run code inside a hidden Electron window
JavaScript
52
star
14

0x10code

💻 A website for sharing and developing code for DCPU16, from Mojang's game 0x10c.
JavaScript
49
star
15

bitcoin-net

🌐 Bitcoin networking that works in Node and the browser
JavaScript
23
star
16

tendermint-node

Run a Tendermint full node from Node.js
JavaScript
22
star
17

blockchain-spv

◾◾◾ Stores blockchain headers and verifies transactions with SPV
JavaScript
22
star
18

if

Conditional branching for Node.js
JavaScript
22
star
19

muta

Mutate your objects without mutating your objects
JavaScript
19
star
20

bitcoin-protocol

🔣 Bitcoin network protocol streams
JavaScript
14
star
21

bitcoin-merkle-proof

🌲 Build and verify Bitcoin Merkle proofs
JavaScript
12
star
22

htlc

Hashed TimeLock Contracts for Coins/Lotion
JavaScript
11
star
23

node-bitcoind

Spawn a Bitcoin Core full node from Node.js
JavaScript
11
star
24

old

Make the 'new' keyword optional for ES6 classes
JavaScript
11
star
25

blockchain-download

Download blockchain data from peers
JavaScript
11
star
26

filecoin-whitepaper

NOTICE: This is not associated with the Filecoin project made by Protocol Labs, and is not being actively worked on
HTML
10
star
27

js-hyperx

Atom syntax highlighting for hyperx
CoffeeScript
10
star
28

webcoin-bitcoin

Bitcoin parameters for Webcoin
JavaScript
8
star
29

modbox

Securely box untrusted JS modules, with CPU and memory limiting
JavaScript
8
star
30

proxmise

Proxied Promises - Create async getters for arbitrary object paths
JavaScript
8
star
31

blockchain-state

A writable stream for applications that consume blocks
JavaScript
6
star
32

coinmarketcat

CryptoKitties market data
JavaScript
6
star
33

bitcoin-inventory

Exchange transactions with peers
JavaScript
5
star
34

bitcoin-wallet

Simple HD wallet
JavaScript
5
star
35

node-sock

Simple networking for Node.js
CoffeeScript
4
star
36

deterministic-json

Deterministic JSON parse/stringify, with support for Buffers
JavaScript
4
star
37

event-cleanup

Wrap an EventEmitter for easy listener cleanup
JavaScript
4
star
38

bitcoin-filter

Bitcoin connection Bloom filtering (BIP37)
JavaScript
4
star
39

bitcoin-util

Utility functions for Bitcoin hashes and targets
JavaScript
4
star
40

on-object

Register many EventEmitter listeners at once using objects
JavaScript
4
star
41

js-crypto

JS equivalent of Tendermint's go-crypto package
JavaScript
4
star
42

staking

Proof-of-stake middleware for Lotion
JavaScript
4
star
43

airwave

An SDR application built on Electron
JavaScript
3
star
44

peerhub

Peer discovery and signalling for WebRTC P2P networks
JavaScript
3
star
45

fft-stream

A stream that transforms SDR I/Q samples into the frequency domain
JavaScript
2
star
46

species

My entry for the 24th Ludum Dare compo.
JavaScript
2
star
47

ipfs-code-viewer

View code in IPFS with automatic syntax highlighting
CSS
2
star
48

qwoptimizer

AI that learns to play QWOP using machine learning
JavaScript
2
star
49

outfitly

The social network for clothes. This app uses node.js, express, and MongoDB.
JavaScript
2
star
50

toronto

A lisp that compiles to JS
JavaScript
1
star
51

webcoin-bitcoin-testnet

Bitcoin testnet3 parameters for webcoin
JavaScript
1
star
52

dstate

Store changing state data as commits that can be rolled back
JavaScript
1
star
53

js-merkleeyes

Client for Tendermint's merkleeyes KV store
JavaScript
1
star
54

tenderdash

A dashboard for your Tendermint node
JavaScript
1
star
55

mongorest

Connect/Express middleware that lets you easily provide a REST API for your MongoDB data.
JavaScript
1
star
56

npmscan

Scans NPM for available package names
JavaScript
1
star
57

aptt-blog

The official blog of Aptt.
1
star
58

versionbits

Track Bitcoin versionbits deployments (BIP9)
JavaScript
1
star
59

number-script

Ordinal JavaScript
JavaScript
1
star
60

aoc-2019

Advent of Code 2019
Rust
1
star
61

mercuryex.com

The website for Mercury
HTML
1
star
62

versionbits-web

A web viewer for Bitcoin versionbits deployments
JavaScript
1
star
63

webcoin-bridge

🌉 Bridge connections from the Websocket/WebRTC network to the TCP network
JavaScript
1
star