Skip to content

Commit 2326ce4

Browse files
committed
docs: update chat example and add info to its readme
docs: update echo example docs: update libp2p in browser example docs: update pubsub example docs: update peer and content routing examples docs: update discovery mechanisms example docs: update encrypted comms example docs: update protocol and stream muxing example
1 parent 8372433 commit 2326ce4

File tree

24 files changed

+401
-241
lines changed

24 files changed

+401
-241
lines changed

examples/chat/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
# Chat example with libp2p
2+
3+
This example creates a simple chat app in your terminal.
4+
5+
## Setup
6+
1. Install the modules, `npm install`.
7+
2. Open 2 terminal windows in the `./src` directory.
8+
9+
## Running
10+
1. Run the listener in window 1, `node listener.js`
11+
2. Run the dialer in window 2, `node dialer.js`
12+
3. Type a message in either window and hit _enter_
13+
4. Tell youself secrets to your hearts content!

examples/chat/src/dialer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ async.parallel([
3131
if (err) throw err
3232
const peerDialer = new PeerInfo(ids[0])
3333
peerDialer.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
34-
const nodeDialer = new Node(peerDialer)
34+
const nodeDialer = new Node({
35+
peerInfo: peerDialer
36+
})
3537

3638
const peerListener = new PeerInfo(ids[1])
3739
idListener = ids[1]

examples/chat/src/libp2p-bundle.js

+33-36
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
const TCP = require('libp2p-tcp')
44
const MulticastDNS = require('libp2p-mdns')
55
const WS = require('libp2p-websockets')
6-
const Railing = require('libp2p-railing')
6+
const Bootstrap = require('libp2p-railing')
77
const spdy = require('libp2p-spdy')
88
const KadDHT = require('libp2p-kad-dht')
99
const mplex = require('libp2p-mplex')
1010
const secio = require('libp2p-secio')
11+
const defaultsDeep = require('@nodeutils/defaults-deep')
1112
const libp2p = require('../../..')
1213

1314
function mapMuxers (list) {
@@ -36,44 +37,40 @@ function getMuxers (muxers) {
3637
}
3738

3839
class Node extends libp2p {
39-
constructor (peerInfo, peerBook, options) {
40-
options = options || {}
41-
42-
const modules = {
43-
transport: [
44-
new TCP(),
45-
new WS()
46-
],
47-
connection: {
48-
muxer: getMuxers(options.muxer),
49-
crypto: [ secio ]
40+
constructor (_options) {
41+
const defaults = {
42+
modules: {
43+
transport: [
44+
TCP,
45+
WS
46+
],
47+
streamMuxer: getMuxers(_options.muxer),
48+
connEncryption: [ secio ],
49+
peerDiscovery: [
50+
MulticastDNS,
51+
Bootstrap
52+
],
53+
dht: KadDHT
5054
},
51-
discovery: []
52-
}
53-
54-
if (options.dht) {
55-
modules.DHT = KadDHT
56-
}
57-
58-
if (options.mdns) {
59-
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
60-
modules.discovery.push(mdns)
61-
}
62-
63-
if (options.bootstrap) {
64-
const r = new Railing(options.bootstrap)
65-
modules.discovery.push(r)
66-
}
67-
68-
if (options.modules && options.modules.transport) {
69-
options.modules.transport.forEach((t) => modules.transport.push(t))
70-
}
71-
72-
if (options.modules && options.modules.discovery) {
73-
options.modules.discovery.forEach((d) => modules.discovery.push(d))
55+
config: {
56+
peerDiscovery: {
57+
mdns: {
58+
interval: 10000,
59+
enabled: false
60+
},
61+
bootstrap: {
62+
interval: 10000,
63+
enabled: false,
64+
list: _options.bootstrapList
65+
}
66+
},
67+
dht: {
68+
kBucketSize: 20
69+
}
70+
}
7471
}
7572

76-
super(modules, peerInfo, peerBook, options)
73+
super(defaultsDeep(_options, defaults))
7774
}
7875
}
7976

examples/chat/src/listener.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
1414
}
1515
const peerListener = new PeerInfo(idListener)
1616
peerListener.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
17-
const nodeListener = new Node(peerListener)
17+
const nodeListener = new Node({
18+
peerInfo: peerListener
19+
})
1820

1921
nodeListener.start((err) => {
2022
if (err) {
2123
throw err
2224
}
2325

24-
nodeListener.switch.on('peer-mux-established', (peerInfo) => {
26+
nodeListener.on('peer:connect', (peerInfo) => {
2527
console.log(peerInfo.id.toB58String())
2628
})
2729

examples/discovery-mechanisms/1.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

3-
const libp2p = require('libp2p')
3+
const libp2p = require('../../')
44
const TCP = require('libp2p-tcp')
55
const Mplex = require('libp2p-mplex')
66
const SECIO = require('libp2p-secio')
77
const PeerInfo = require('peer-info')
8-
const Railing = require('libp2p-railing')
8+
const Bootstrap = require('libp2p-railing')
99
const waterfall = require('async/waterfall')
10+
const defaultsDeep = require('@nodeutils/defaults-deep')
1011

1112
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json
1213
const bootstrapers = [
@@ -22,16 +23,26 @@ const bootstrapers = [
2223
]
2324

2425
class MyBundle extends libp2p {
25-
constructor (peerInfo) {
26-
const modules = {
27-
transport: [new TCP()],
28-
connection: {
29-
muxer: [Mplex],
30-
crypto: [SECIO]
26+
constructor (_options) {
27+
const defaults = {
28+
modules: {
29+
transport: [ TCP ],
30+
streamMuxer: [ Mplex ],
31+
connEncryption: [ SECIO ],
32+
peerDiscovery: [ Bootstrap ]
3133
},
32-
discovery: [new Railing(bootstrapers)]
34+
config: {
35+
peerDiscovery: {
36+
bootstrap: {
37+
interval: 2000,
38+
enabled: true,
39+
list: bootstrapers
40+
}
41+
}
42+
}
3343
}
34-
super(modules, peerInfo)
44+
45+
super(defaultsDeep(_options, defaults))
3546
}
3647
}
3748

@@ -41,7 +52,9 @@ waterfall([
4152
(cb) => PeerInfo.create(cb),
4253
(peerInfo, cb) => {
4354
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
44-
node = new MyBundle(peerInfo)
55+
node = new MyBundle({
56+
peerInfo
57+
})
4558
node.start(cb)
4659
}
4760
], (err) => {

examples/discovery-mechanisms/2.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
'use strict'
22

3-
const libp2p = require('libp2p')
3+
const libp2p = require('../../')
44
const TCP = require('libp2p-tcp')
55
const Mplex = require('libp2p-mplex')
66
const SECIO = require('libp2p-secio')
77
const PeerInfo = require('peer-info')
88
const MulticastDNS = require('libp2p-mdns')
99
const waterfall = require('async/waterfall')
1010
const parallel = require('async/parallel')
11+
const defaultsDeep = require('@nodeutils/defaults-deep')
1112

1213
class MyBundle extends libp2p {
13-
constructor (peerInfo) {
14-
const modules = {
15-
transport: [new TCP()],
16-
connection: {
17-
muxer: [Mplex],
18-
crypto: [SECIO]
14+
constructor (_options) {
15+
const defaults = {
16+
modules: {
17+
transport: [ TCP ],
18+
streamMuxer: [ Mplex ],
19+
connEncryption: [ SECIO ],
20+
peerDiscovery: [ MulticastDNS ]
1921
},
20-
discovery: [new MulticastDNS(peerInfo, { interval: 1000 })]
22+
config: {
23+
peerDiscovery: {
24+
mdns: {
25+
interval: 1000,
26+
enabled: true
27+
}
28+
}
29+
}
2130
}
22-
super(modules, peerInfo)
31+
32+
super(defaultsDeep(_options, defaults))
2333
}
2434
}
2535

@@ -30,7 +40,9 @@ function createNode (callback) {
3040
(cb) => PeerInfo.create(cb),
3141
(peerInfo, cb) => {
3242
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
33-
node = new MyBundle(peerInfo)
43+
node = new MyBundle({
44+
peerInfo
45+
})
3446
node.start(cb)
3547
}
3648
], (err) => callback(err, node))

examples/discovery-mechanisms/README.md

+38-17
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,28 @@ For this demo, we will connect to IPFS default bootstrapper nodes and so, we wil
1313
First, we create our libp2p bundle.
1414

1515
```JavaScript
16+
const Bootstrap = require('libp2p-railing')
1617
class MyBundle extends libp2p {
1718
constructor (peerInfo) {
18-
const modules = {
19-
transport: [new TCP()],
20-
connection: {
21-
muxer: [Mplex],
22-
crypto: [SECIO]
19+
const defaults = {
20+
modules: {
21+
transport: [ TCP ],
22+
streamMuxer: [ Mplex ],
23+
connEncryption: [ SECIO ],
24+
peerDiscovery: [ Bootstrap ]
2325
},
24-
discovery: [new Railing(bootstrapers)]
26+
config: {
27+
peerDiscovery: {
28+
bootstrap: {
29+
interval: 2000,
30+
enabled: true,
31+
list: bootstrapers
32+
}
33+
}
34+
}
2535
}
26-
super(modules, peerInfo)
36+
37+
super(defaultsDeep(_options, defaults))
2738
}
2839
}
2940
```
@@ -53,7 +64,9 @@ waterfall([
5364
(cb) => PeerInfo.create(cb),
5465
(peerInfo, cb) => {
5566
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
56-
node = new MyBundle(peerInfo)
67+
node = new MyBundle({
68+
peerInfo
69+
})
5770
node.start(cb)
5871
}
5972
], (err) => {
@@ -108,17 +121,25 @@ Update your libp2p bundle to include MulticastDNS.
108121
```JavaScript
109122
class MyBundle extends libp2p {
110123
constructor (peerInfo) {
111-
const modules = {
112-
transport: [new TCP()],
113-
connection: {
114-
muxer: [Mplex],
115-
crypto: [SECIO]
124+
const defaults = {
125+
modules: {
126+
transport: [ TCP ],
127+
streamMuxer: [ Mplex ],
128+
connEncryption: [ SECIO ],
129+
peerDiscovery: [ MulticastDNS ]
116130
},
117-
// We set the interval here to 1 second so that is faster to observe. The
118-
// default is 10 seconds.
119-
discovery: [new MulticastDNS(peerInfo, { interval: 1000 })]
131+
config: {
132+
peerDiscovery: {
133+
mdns: {
134+
// Run at 1s so we can observe more quickly, default is 10s
135+
interval: 1000,
136+
enabled: true
137+
}
138+
}
139+
}
120140
}
121-
super(modules, peerInfo)
141+
142+
super(defaultsDeep(_options, defaults))
122143
}
123144
}
124145
```

examples/echo/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
# Echo example with libp2p
2+
3+
This example performs a simple echo from the listener to the dialer.
4+
5+
## Setup
6+
1. Install the modules, `npm install`.
7+
2. Open 2 terminal windows in the `./src` directory.
8+
9+
## Running
10+
1. Run the listener in window 1, `node listener.js`
11+
2. Run the dialer in window 2, `node dialer.js`
12+
3. You should see console logs showing the dial, and the received echo of _hey_
13+
4. If you look at the listener window, you will see it receiving the dial

examples/echo/src/dialer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ async.parallel([
2121
const dialerId = ids[0]
2222
const dialerPeerInfo = new PeerInfo(dialerId)
2323
dialerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
24-
const dialerNode = new Node(dialerPeerInfo)
24+
const dialerNode = new Node({
25+
peerInfo: dialerPeerInfo
26+
})
2527

2628
// Peer to Dial
2729
const listenerPeerInfo = new PeerInfo(ids[1])

0 commit comments

Comments
 (0)