Skip to content

Commit 2b02be0

Browse files
committed
docs(examples): transports 2
1 parent 32cb59f commit 2b02be0

File tree

3 files changed

+201
-1
lines changed

3 files changed

+201
-1
lines changed

examples/transports/2.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict'
2+
3+
const libp2p = require('libp2p')
4+
const TCP = require('libp2p-tcp')
5+
const PeerInfo = require('peer-info')
6+
const waterfall = require('async/waterfall')
7+
const parallel = require('async/parallel')
8+
const pull = require('pull-stream')
9+
10+
class MyBundle extends libp2p {
11+
constructor (peerInfo) {
12+
const modules = {
13+
transport: [new TCP()]
14+
}
15+
super(modules, peerInfo)
16+
}
17+
}
18+
19+
function createNode (callback) {
20+
let node
21+
22+
waterfall([
23+
(cb) => PeerInfo.create(cb),
24+
(peerInfo, cb) => {
25+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
26+
node = new MyBundle(peerInfo)
27+
node.start(cb)
28+
}
29+
], (err) => callback(err, node))
30+
}
31+
32+
function printAddrs (node, number) {
33+
console.log('node %s is listening on:', number)
34+
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
35+
}
36+
37+
parallel([
38+
(cb) => createNode(cb),
39+
(cb) => createNode(cb)
40+
], (err, nodes) => {
41+
if (err) { throw err }
42+
43+
const node1 = nodes[0]
44+
const node2 = nodes[1]
45+
46+
printAddrs(node1, '1')
47+
printAddrs(node2, '2')
48+
49+
node2.handle('/print', (protocol, conn) => {
50+
pull(
51+
conn,
52+
pull.map((v) => v.toString()),
53+
pull.log()
54+
)
55+
})
56+
57+
node1.dial(node2.peerInfo, '/print', (err, conn) => {
58+
if (err) { throw err }
59+
60+
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
61+
})
62+
})

examples/transports/3.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict'
2+
3+
const libp2p = require('libp2p')
4+
const TCP = require('libp2p-tcp')
5+
const PeerInfo = require('peer-info')
6+
const waterfall = require('async/waterfall')
7+
const parallel = require('async/parallel')
8+
const pull = require('pull-stream')
9+
10+
class MyBundle extends libp2p {
11+
constructor (peerInfo) {
12+
const modules = {
13+
transport: [new TCP()]
14+
}
15+
super(modules, peerInfo)
16+
}
17+
}
18+
19+
function createNode (callback) {
20+
let node
21+
22+
waterfall([
23+
(cb) => PeerInfo.create(cb),
24+
(peerInfo, cb) => {
25+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
26+
node = new MyBundle(peerInfo)
27+
node.start(cb)
28+
}
29+
], (err) => callback(err, node))
30+
}
31+
32+
function printAddrs (node, number) {
33+
console.log('node %s is listening on:', number)
34+
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
35+
}
36+
37+
parallel([
38+
(cb) => createNode(cb),
39+
(cb) => createNode(cb)
40+
], (err, nodes) => {
41+
if (err) { throw err }
42+
43+
const node1 = nodes[0]
44+
const node2 = nodes[1]
45+
46+
printAddrs(node1, '1')
47+
printAddrs(node2, '2')
48+
49+
node2.handle('/print', (protocol, conn) => {
50+
pull(
51+
conn,
52+
pull.map((v) => v.toString()),
53+
pull.log()
54+
)
55+
})
56+
57+
node1.dial(node2.peerInfo, '/print', (err, conn) => {
58+
if (err) { throw err }
59+
60+
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
61+
})
62+
})

examples/transports/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,87 @@ That `QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ` is the PeerId that was cre
9292

9393
Now that we have our bundle, let's create two nodes and make them dial to each other! You can find the complete solution at [2.js](/2.js).
9494

95+
For this step, we will need one more dependency.
9596

97+
```bash
98+
> npm install pull-stream
99+
```
100+
101+
We are going to reuse the MyBundle class from step 1, but this time to make things simpler, we will create two functions, one to create nodes and another to print the addrs to avoid duplicating code.
102+
103+
```JavaScript
104+
function createNode (callback) {
105+
let node
106+
107+
waterfall([
108+
(cb) => PeerInfo.create(cb),
109+
(peerInfo, cb) => {
110+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
111+
node = new MyBundle(peerInfo)
112+
node.start(cb)
113+
}
114+
], (err) => callback(err, node))
115+
}
116+
117+
function printAddrs (node, number) {
118+
console.log('node %s is listening on:', number)
119+
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
120+
}
121+
```
122+
123+
Now we are going to use `async/parallel` to create two nodes, print their addresses and dial from one node to the other.
124+
125+
```
126+
parallel([
127+
(cb) => createNode(cb),
128+
(cb) => createNode(cb)
129+
], (err, nodes) => {
130+
if (err) { throw err }
131+
132+
const node1 = nodes[0]
133+
const node2 = nodes[1]
134+
135+
printAddrs(node1, '1')
136+
printAddrs(node2, '2')
137+
138+
node2.handle('/print', (protocol, conn) => {
139+
pull(
140+
conn,
141+
pull.map((v) => v.toString()),
142+
pull.log()
143+
)
144+
})
145+
146+
node1.dial(node2.peerInfo, '/print', (err, conn) => {
147+
if (err) { throw err }
148+
149+
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
150+
})
151+
})
152+
```
153+
154+
The result should be look like:
155+
156+
```bash
157+
> node 2.js
158+
node 1 is listening on:
159+
/ip4/127.0.0.1/tcp/62279/ipfs/QmeM4wNWv1uci7UJjUXZYfvcy9uqAbw7G9icuxdqy88Mj9
160+
/ip4/192.168.2.156/tcp/62279/ipfs/QmeM4wNWv1uci7UJjUXZYfvcy9uqAbw7G9icuxdqy88Mj9
161+
node 2 is listening on:
162+
/ip4/127.0.0.1/tcp/62278/ipfs/QmWp58xJgzbouNJcyiNNTpZuqQCJU8jf6ixc7TZT9xEZhV
163+
/ip4/192.168.2.156/tcp/62278/ipfs/QmWp58xJgzbouNJcyiNNTpZuqQCJU8jf6ixc7TZT9xEZhV
164+
Hello p2p world!
165+
```
96166

97167
## 3. Using multiple transports
98168

99-
- show TCP + websockets
169+
Next, we want to be available in multiple transports to increase our chances of having common transports in the network. A simple scenario, a node running in the browser only has access to HTTP, WebSockets and WebRTC since the browser doesn't let you open any other kind of transport, for this node to dial to some other node, that other node needs to share a common transport.
170+
171+
What we are going to do in this step is to create 3 nodes, one with TCP, another with TCP+WebSockets and another one with just WebSockets. The full solution can be found on [3.js](3.js)
172+
173+
AKLSDJAKLSDJAKLSDJAKLSJ
174+
175+
100176

101177
## 4. How to create a new libp2p transport
102178

0 commit comments

Comments
 (0)