👩↔️👨 Decentralized peer discovery and signaling
Switch branches/tags
greenkeeper-babel-cli-6.9.0 greenkeeper-babel-cli-6.11.4 greenkeeper-babel-cli-6.16.0 greenkeeper-babel-cli-6.22.0 greenkeeper-babel-cli-6.22.1 greenkeeper-babel-cli-6.22.2 greenkeeper-babel-cli-6.23.0 greenkeeper-babel-cli-6.24.0 greenkeeper-babel-preset-es2015-6.22.0 greenkeeper-babel-preset-es2015-6.24.0 greenkeeper-debug-2.3.1 greenkeeper-debug-2.3.2 greenkeeper-debug-2.4.0 greenkeeper-debug-2.5.0 greenkeeper-debug-2.6.1 greenkeeper-debug-2.6.2 greenkeeper-debug-2.6.3 greenkeeper-debug-2.6.7 greenkeeper-debug-2.6.8 greenkeeper-dependency-check-2.8.0 greenkeeper-nyc-7.0.0 greenkeeper-nyc-7.1.0 greenkeeper-nyc-8.0.0 greenkeeper-nyc-8.1.0 greenkeeper-nyc-8.3.0 greenkeeper-nyc-8.3.1 greenkeeper-nyc-8.3.2 greenkeeper-nyc-8.4.0 greenkeeper-nyc-9.0.1 greenkeeper-nyc-10.0.0 greenkeeper-nyc-10.1.0 greenkeeper-nyc-10.1.2 greenkeeper-nyc-10.2.0 greenkeeper-nyc-10.3.0 greenkeeper-nyc-10.3.2 greenkeeper-object-assign-4.1.1 greenkeeper-old-0.2.0 greenkeeper-simple-peer-6.1.1 greenkeeper-simple-peer-6.1.2 greenkeeper-simple-peer-6.1.3 greenkeeper-simple-peer-6.1.4 greenkeeper-simple-peer-6.1.5 greenkeeper-simple-peer-6.2.0 greenkeeper-simple-peer-6.2.1 greenkeeper-simple-peer-6.2.2 greenkeeper-simple-peer-6.3.0 greenkeeper-simple-peer-6.4.0 greenkeeper-simple-peer-6.4.1 greenkeeper-simple-peer-6.4.2 greenkeeper-simple-peer-6.4.3 greenkeeper-simple-peer-6.4.4 greenkeeper-simple-peer-7.0.0 greenkeeper-simple-peer-7.0.1 greenkeeper-simple-peer-8.0.0 greenkeeper-simple-peer-8.1.0 greenkeeper-standard-7.1.1 greenkeeper-standard-8.0.0 greenkeeper-standard-8.1.0 greenkeeper-standard-8.2.0 greenkeeper-standard-8.3.0 greenkeeper-standard-8.4.0 greenkeeper-standard-9.0.0 greenkeeper-standard-9.0.2 greenkeeper-standard-10.0.0 greenkeeper-standard-10.0.1 greenkeeper-standard-10.0.2 greenkeeper-update-all greenkeeper-websocket-stream-3.3.0 greenkeeper-websocket-stream-3.3.2 greenkeeper-websocket-stream-4.0.0 greenkeeper-websocket-stream-5.0.0 greenkeeper-wrtc-0.0.62 master stream-response v2
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



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.


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.

  // 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, '', () => ex.connect(socket))

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



var Exchange = require('peer-exchange')


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.


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).


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).



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


The network ID provided in the constructor.


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.