Skip to content

Commit

Permalink
docs: update examples to 0.28.x api (#625)
Browse files Browse the repository at this point in the history
* chore: update examples to 0.28 api

* chore: use libp2p-noise in examples

* chore: examples using multiaddrs property of libp2p

Co-authored-by: Jacob Heun <jacobheun@gmail.com>

* docs: update language around secio in crypto example

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
vasco-santos and jacobheun authored May 15, 2020
1 parent 37636ea commit efc374e
Show file tree
Hide file tree
Showing 30 changed files with 298 additions and 249 deletions.
20 changes: 9 additions & 11 deletions examples/chat/src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-console */

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const Node = require('./libp2p-bundle')
const { stdinToStream, streamToConsole } = require('./stream')

Expand All @@ -13,27 +13,25 @@ async function run() {
])

// Create a new libp2p node on localhost with a randomly chosen port
const peerDialer = new PeerInfo(idDialer)
peerDialer.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
const nodeDialer = new Node({
peerInfo: peerDialer
peerId: idDialer,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
}
})

// Create a PeerInfo with the listening peer's address
const peerListener = new PeerInfo(idListener)
peerListener.multiaddrs.add('/ip4/127.0.0.1/tcp/10333')

// Start the libp2p host
await nodeDialer.start()

// Output this node's address
console.log('Dialer ready, listening on:')
peerListener.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
nodeDialer.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/p2p/' + idDialer.toB58String())
})

// Dial to the remote peer (the "listener")
const { stream } = await nodeDialer.dialProtocol(peerListener, '/chat/1.0.0')
const listenerMa = multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toB58String()}`)
const { stream } = await nodeDialer.dialProtocol(listenerMa, '/chat/1.0.0')

console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
console.log('Type a message and see what happens')
Expand Down
5 changes: 3 additions & 2 deletions examples/chat/src/libp2p-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const mplex = require('libp2p-mplex')
const secio = require('libp2p-secio')
const SECIO = require('libp2p-secio')
const { NOISE } = require('libp2p-noise')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../..')

Expand All @@ -16,7 +17,7 @@ class Node extends libp2p {
WS
],
streamMuxer: [ mplex ],
connEncryption: [ secio ]
connEncryption: [ NOISE, SECIO ]
}
}

Expand Down
15 changes: 8 additions & 7 deletions examples/chat/src/listener.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
'use strict'
/* eslint-disable no-console */

const multaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('./libp2p-bundle.js')
const { stdinToStream, streamToConsole } = require('./stream')

async function run() {
// Create a new libp2p node with the given multi-address
const idListener = await PeerId.createFromJSON(require('./peer-id-listener'))
const peerListener = new PeerInfo(idListener)
peerListener.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
const nodeListener = new Node({
peerInfo: peerListener
peerId: idListener,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/10333']
}
})

// Log a message when a remote peer connects to us
nodeListener.on('peer:connect', (peerInfo) => {
console.log(peerInfo.id.toB58String())
nodeListener.connectionManager.on('peer:connect', (connection) => {
console.log('connected to: ', connection.remotePeer.toB58String())
})

// Handle messages for the protocol
Expand All @@ -33,7 +34,7 @@ async function run() {

// Output listen addresses to the console
console.log('Listener ready, listening on:')
peerListener.multiaddrs.forEach((ma) => {
nodeListener.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
})
}
Expand Down
16 changes: 9 additions & 7 deletions examples/discovery-mechanisms/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const { NOISE } = require('libp2p-noise')
const Bootstrap = require('libp2p-bootstrap')

// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json
Expand All @@ -22,10 +23,13 @@ const bootstrapers = [

;(async () => {
const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [SECIO],
connEncryption: [NOISE, SECIO],
peerDiscovery: [Bootstrap]
},
config: {
Expand All @@ -39,15 +43,13 @@ const bootstrapers = [
}
})

node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')

node.on('peer:connect', (peer) => {
console.log('Connection established to:', peer.id.toB58String()) // Emitted when a peer has been found
node.connectionManager.on('peer:connect', (connection) => {
console.log('Connection established to:', connection.remotePeer.toB58String()) // Emitted when a peer has been found
})

node.on('peer:discovery', (peer) => {
node.on('peer:discovery', (peerId) => {
// No need to dial, autoDial is on
console.log('Discovered:', peer.id.toB58String())
console.log('Discovered:', peerId.toB58String())
})

await node.start()
Expand Down
11 changes: 7 additions & 4 deletions examples/discovery-mechanisms/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ const Libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const { NOISE } = require('libp2p-noise')
const MulticastDNS = require('libp2p-mdns')

const createNode = async () => {
const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [SECIO],
connEncryption: [NOISE, SECIO],
peerDiscovery: [MulticastDNS]
},
config: {
Expand All @@ -24,7 +28,6 @@ const createNode = async () => {
}
}
})
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')

return node
}
Expand All @@ -35,8 +38,8 @@ const createNode = async () => {
createNode()
])

node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
node1.on('peer:discovery', (peerId) => console.log('Discovered:', peerId.toB58String()))
node2.on('peer:discovery', (peerId) => console.log('Discovered:', peerId.toB58String()))

await Promise.all([
node1.start(),
Expand Down
37 changes: 25 additions & 12 deletions examples/discovery-mechanisms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A Peer Discovery module enables libp2p to find peers to connect to. Think of the

With these system, a libp2p node can both have a set of nodes to always connect on boot (bootstraper nodes), discover nodes through locality (e.g connected in the same LAN) or through serendipity (random walks on a DHT).

These mechanisms save configuration and enable a node to operate without any explicit dials, it will just work.
These mechanisms save configuration and enable a node to operate without any explicit dials, it will just work. Once new peers are discovered, their known data is stored in the peer's PeerStore.

## 1. Bootstrap list of Peers when booting a node

Expand All @@ -20,7 +20,7 @@ const node = Libp2p.create({
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
connEncryption: [ NOISE, SECIO ],
peerDiscovery: [ Bootstrap ]
},
config: {
Expand Down Expand Up @@ -55,11 +55,14 @@ Now, once we create and start the node, we can listen for events such as `peer:d

```JavaScript
const node = await Libp2p.create({
peerInfo,
peerId,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
}
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
connEncryption: [ NOISE, SECIO ],
peerDiscovery: [ Bootstrap ]
},
config: {
Expand All @@ -73,15 +76,13 @@ const node = await Libp2p.create({
}
})

node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')

node.on('peer:connect', (peer) => {
console.log('Connection established to:', peer.id.toB58String()) // Emitted when a peer has been found
node.connectionManager.on('peer:connect', (connection) => {
console.log('Connection established to:', connection.remotePeer.toB58String()) // Emitted when a new connection has been created
})

// Emitted when a peer has been found
node.on('peer:discovery', (peer) => {
console.log('Discovered:', peer.id.toB58String())
node.on('peer:discovery', (peerId) => {
// No need to dial, autoDial is on
console.log('Discovered:', peerId.toB58String())
})

await node.start()
Expand All @@ -100,6 +101,15 @@ Discovered: QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64
Discovered: QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd
Discovered: QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3
Discovered: QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
Connection established to: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
Connection established to: QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z
Connection established to: QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM
Connection established to: QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm
Connection established to: QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu
Connection established to: QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64
Connection established to: QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd
Connection established to: QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3
Connection established to: QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx
```

## 2. MulticastDNS to find other peers in the network
Expand All @@ -114,10 +124,13 @@ const MulticastDNS = require('libp2p-mdns')

const createNode = () => {
return Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
}
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
connEncryption: [ NOISE, SECIO ],
peerDiscovery: [ MulticastDNS ]
},
config: {
Expand Down
22 changes: 10 additions & 12 deletions examples/echo/src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Dialer Node
*/

const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('./libp2p-bundle')
const pipe = require('it-pipe')

Expand All @@ -17,28 +17,26 @@ async function run() {
])

// Dialer
const dialerPeerInfo = new PeerInfo(dialerId)
dialerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
const dialerNode = new Node({
peerInfo: dialerPeerInfo
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
peerId: dialerId
})

// Peer to Dial (the listener)
const listenerPeerInfo = new PeerInfo(listenerId)
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' +
listenerId.toB58String()
listenerPeerInfo.multiaddrs.add(listenerMultiaddr)
// Add peer to Dial (the listener) into the PeerStore
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' + listenerId.toB58String()

// Start the dialer libp2p node
await dialerNode.start()

console.log('Dialer ready, listening on:')
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
dialerNode.multiaddrs.forEach((ma) => console.log(ma.toString() +
'/p2p/' + dialerId.toB58String()))

// Dial the listener node
console.log('Dialing to peer:', listenerMultiaddr.toString())
const { stream } = await dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0')
console.log('Dialing to peer:', listenerMultiaddr)
const { stream } = await dialerNode.dialProtocol(listenerMultiaddr, '/echo/1.0.0')

console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')

Expand Down
6 changes: 4 additions & 2 deletions examples/echo/src/libp2p-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const mplex = require('libp2p-mplex')
const secio = require('libp2p-secio')
const SECIO = require('libp2p-secio')
const { NOISE } = require('libp2p-noise')

const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../..')

Expand All @@ -16,7 +18,7 @@ class Node extends libp2p {
WS
],
streamMuxer: [ mplex ],
connEncryption: [ secio ]
connEncryption: [ NOISE, SECIO ]
}
}

Expand Down
14 changes: 7 additions & 7 deletions examples/echo/src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
*/

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('./libp2p-bundle')
const pipe = require('it-pipe')

async function run() {
const listenerId = await PeerId.createFromJSON(require('./id-l'))

// Listener libp2p node
const listenerPeerInfo = new PeerInfo(listenerId)
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
const listenerNode = new Node({
peerInfo: listenerPeerInfo
addresses: {
listen: ['/ip4/0.0.0.0/tcp/10333']
},
peerId: listenerId
})

// Log a message when we receive a connection
listenerNode.on('peer:connect', (peerInfo) => {
console.log('received dial to me from:', peerInfo.id.toB58String())
listenerNode.connectionManager.on('peer:connect', (connection) => {
console.log('received dial to me from:', connection.remotePeer.toB58String())
})

// Handle incoming connections for the protocol by piping from the stream
Expand All @@ -33,7 +33,7 @@ async function run() {
await listenerNode.start()

console.log('Listener ready, listening on:')
listenerNode.peerInfo.multiaddrs.forEach((ma) => {
listenerNode.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/p2p/' + listenerId.toB58String())
})
}
Expand Down
Loading

0 comments on commit efc374e

Please sign in to comment.