Skip to content

Commit 547c4db

Browse files
authored
docs: refactor examples (#96)
1 parent 606fa73 commit 547c4db

20 files changed

+219
-26
lines changed

examples/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Let us know if you find any issue or if you want to contribute and add a new tut
66

77
## Examples
88

9-
- [In Node.js](./nodejs)
10-
- [echo](./nodejs/echo)
11-
- [chat](./nodejs/chat)
12-
- [In the browser](./browser)
13-
- [mapper](./browser/mapper)
9+
- [The standard echo net example with libp2p](./echo)
10+
- [A simple chat app with](./chat)
11+
- [See other nodes in the network using WebRTC Star discovery mechanism](./see-nodes)

examples/chat/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Chat example with libp2p

examples/nodejs/chat/dialer.js renamed to examples/chat/src/dialer.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
const PeerId = require('peer-id')
55
const PeerInfo = require('peer-info')
6-
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
7-
const multiaddr = require('multiaddr')
6+
const Node = require('./libp2p-bundle')
87
const pull = require('pull-stream')
98
const async = require('async')
109
const Pushable = require('pull-pushable')
@@ -31,12 +30,12 @@ async.parallel([
3130
], (err, ids) => {
3231
if (err) throw err
3332
const peerDialer = new PeerInfo(ids[0])
34-
peerDialer.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
33+
peerDialer.multiaddr.add('/ip4/0.0.0.0/tcp/0')
3534
const nodeDialer = new Node(peerDialer)
3635

3736
const peerListener = new PeerInfo(ids[1])
3837
idListener = ids[1]
39-
peerListener.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/10333'))
38+
peerListener.multiaddr.add('/ip4/127.0.0.1/tcp/10333')
4039
nodeDialer.start((err) => {
4140
if (err) {
4241
throw err

examples/chat/src/libp2p-bundle.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const TCP = require('libp2p-tcp')
4+
const MulticastDNS = require('libp2p-mdns')
5+
const WS = require('libp2p-websockets')
6+
const Railing = require('libp2p-railing')
7+
const spdy = require('libp2p-spdy')
8+
const KadDHT = require('libp2p-kad-dht')
9+
const multiplex = require('libp2p-multiplex')
10+
const secio = require('libp2p-secio')
11+
const libp2p = require('../..')
12+
13+
function mapMuxers (list) {
14+
return list.map((pref) => {
15+
if (typeof pref !== 'string') {
16+
return pref
17+
}
18+
switch (pref.trim().toLowerCase()) {
19+
case 'spdy': return spdy
20+
case 'multiplex': return multiplex
21+
default:
22+
throw new Error(pref + ' muxer not available')
23+
}
24+
})
25+
}
26+
27+
function getMuxers (muxers) {
28+
const muxerPrefs = process.env.LIBP2P_MUXER
29+
if (muxerPrefs && !muxers) {
30+
return mapMuxers(muxerPrefs.split(','))
31+
} else if (muxers) {
32+
return mapMuxers(muxers)
33+
} else {
34+
return [multiplex, spdy]
35+
}
36+
}
37+
38+
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 ]
50+
},
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))
74+
}
75+
76+
super(modules, peerInfo, peerBook, options)
77+
}
78+
}
79+
80+
module.exports = Node

examples/nodejs/chat/listener.js renamed to examples/chat/src/listener.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
const PeerId = require('peer-id')
55
const PeerInfo = require('peer-info')
6-
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
7-
const multiaddr = require('multiaddr')
6+
const Node = require('./libp2p-bundle.js')
87
const pull = require('pull-stream')
98
const Pushable = require('pull-pushable')
109
const p = Pushable()
@@ -14,7 +13,7 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
1413
throw err
1514
}
1615
const peerListener = new PeerInfo(idListener)
17-
peerListener.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/10333'))
16+
peerListener.multiaddr.add('/ip4/0.0.0.0/tcp/10333')
1817
const nodeListener = new Node(peerListener)
1918

2019
nodeListener.start((err) => {

examples/echo/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Echo example with libp2p

examples/nodejs/echo/dialer.js renamed to examples/echo/src/dialer.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
const PeerId = require('peer-id')
99
const PeerInfo = require('peer-info')
10-
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
11-
const multiaddr = require('multiaddr')
10+
const Node = require('./libp2p-bundle')
1211
const pull = require('pull-stream')
1312
const async = require('async')
1413

@@ -21,14 +20,14 @@ async.parallel([
2120
// Dialer
2221
const dialerId = ids[0]
2322
const dialerPeerInfo = new PeerInfo(dialerId)
24-
dialerPeerInfo.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
23+
dialerPeerInfo.multiaddr.add('/ip4/0.0.0.0/tcp/0')
2524
const dialerNode = new Node(dialerPeerInfo)
2625

2726
// Peer to Dial
2827
const listenerPeerInfo = new PeerInfo(ids[1])
2928
const listenerId = ids[1]
30-
const listenerMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/10333/ipfs/' +
31-
listenerId.toB58String())
29+
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/ipfs/' +
30+
listenerId.toB58String()
3231
listenerPeerInfo.multiaddr.add(listenerMultiaddr)
3332

3433
dialerNode.start((err) => {
File renamed without changes.
File renamed without changes.

examples/echo/src/libp2p-bundle.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const TCP = require('libp2p-tcp')
4+
const MulticastDNS = require('libp2p-mdns')
5+
const WS = require('libp2p-websockets')
6+
const Railing = require('libp2p-railing')
7+
const spdy = require('libp2p-spdy')
8+
const KadDHT = require('libp2p-kad-dht')
9+
const multiplex = require('libp2p-multiplex')
10+
const secio = require('libp2p-secio')
11+
const libp2p = require('../../..')
12+
13+
function mapMuxers (list) {
14+
return list.map((pref) => {
15+
if (typeof pref !== 'string') {
16+
return pref
17+
}
18+
switch (pref.trim().toLowerCase()) {
19+
case 'spdy': return spdy
20+
case 'multiplex': return multiplex
21+
default:
22+
throw new Error(pref + ' muxer not available')
23+
}
24+
})
25+
}
26+
27+
function getMuxers (muxers) {
28+
const muxerPrefs = process.env.LIBP2P_MUXER
29+
if (muxerPrefs && !muxers) {
30+
return mapMuxers(muxerPrefs.split(','))
31+
} else if (muxers) {
32+
return mapMuxers(muxers)
33+
} else {
34+
return [multiplex, spdy]
35+
}
36+
}
37+
38+
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 ]
50+
},
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))
74+
}
75+
76+
super(modules, peerInfo, peerBook, options)
77+
}
78+
}
79+
80+
module.exports = Node

examples/nodejs/echo/listener.js renamed to examples/echo/src/listener.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
const PeerId = require('peer-id')
99
const PeerInfo = require('peer-info')
10-
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
11-
const multiaddr = require('multiaddr')
10+
const Node = require('./libp2p-bundle')
1211
const pull = require('pull-stream')
1312
const series = require('async/series')
1413

@@ -25,15 +24,14 @@ series([
2524
},
2625
(cb) => {
2726
const listenerPeerInfo = new PeerInfo(listenerId)
28-
listenerPeerInfo.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/10333'))
27+
listenerPeerInfo.multiaddr.add('/ip4/0.0.0.0/tcp/10333')
2928
listenerNode = new Node(listenerPeerInfo)
3029

3130
listenerNode.swarm.on('peer-mux-established', (peerInfo) => {
3231
console.log('received dial to me from:', peerInfo.id.toB58String())
3332
})
3433

3534
listenerNode.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
36-
3735
listenerNode.start(cb)
3836
}
3937
], (err) => {
File renamed without changes.

examples/see-nodes/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# See nodes
2+
3+
> See other nodes in the network using WebRTC Star Discovery
4+
5+
# Try it out
6+
7+
```
8+
# After having run `npm install` at the root of the repo.
9+
> npm install
10+
> npm start
11+
# open your browser in port :9090
12+
```

examples/browser/mapper/package.json renamed to examples/see-nodes/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "mapper",
2+
"name": "see-nodes",
33
"version": "0.0.0",
4-
"description": "",
4+
"description": "See other nodes in the network using WebRTC Star discovery mechanism",
55
"main": "src/index.js",
66
"scripts": {
7-
"bundle": "browserify src/index.js --require browserify-zlib-next:zlib > public/bundle.js",
7+
"bundle": "browserify src/index.js > public/bundle.js",
88
"serve": "static public -p 9090 -H '{\"Cache-Control\": \"no-cache, must-revalidate\"}'",
99
"mon": "nodemon --exec \"npm run start\" --ignore public/bundle.js",
1010
"start": "npm run bundle && npm run serve"
@@ -13,7 +13,6 @@
1313
"devDependencies": {
1414
"browserify": "^14.0.0",
1515
"browserify-optional": "^1.0.0",
16-
"browserify-zlib-next": "^1.0.1",
1716
"concat-stream": "^1.6.0",
1817
"detect-dom-ready": "^1.0.2",
1918
"node-static": "^0.7.9",

examples/browser/mapper/src/create-node.js renamed to examples/see-nodes/src/create-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const PeerInfo = require('peer-info')
4-
const Node = require('../../../../test/browser-bundle/browser-bundle.js')
4+
const Node = require('./libp2p-bundle')
55

66
function createNode (callback) {
77
PeerInfo.create((err, peerInfo) => {
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const WebRTCStar = require('libp2p-webrtc-star')
4+
const multiplex = require('libp2p-multiplex')
5+
const spdy = require('libp2p-spdy')
6+
const secio = require('libp2p-secio')
7+
const libp2p = require('../../..')
8+
9+
class Node extends libp2p {
10+
constructor (peerInfo, peerBook, options) {
11+
options = options || {}
12+
const wstar = new WebRTCStar()
13+
14+
const modules = {
15+
transport: [wstar],
16+
connection: {
17+
muxer: [multiplex, spdy],
18+
crypto: [secio]
19+
},
20+
discovery: [wstar.discovery]
21+
}
22+
23+
super(modules, peerInfo, peerBook, options)
24+
}
25+
}
26+
27+
module.exports = Node

0 commit comments

Comments
 (0)