Skip to content

Commit 7320495

Browse files
authored
docs: use Libp2p.create() in examples (#811) (#814)
1 parent e9e4b73 commit 7320495

File tree

7 files changed

+67
-47
lines changed

7 files changed

+67
-47
lines changed

doc/API.md

+39-8
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,25 @@ For Libp2p configurations and modules details read the [Configuration Document](
114114

115115
```js
116116
const Libp2p = require('libp2p')
117+
const TCP = require('libp2p-tcp')
118+
const MPLEX = require('libp2p-mplex')
119+
const { NOISE } = require('libp2p-noise')
120+
121+
async function main () {
122+
// specify options
123+
const options = {
124+
modules: {
125+
transport: [TCP],
126+
streamMuxer: [MPLEX],
127+
connEncryption: [NOISE]
128+
}
129+
}
117130

118-
// specify options
119-
const options = {}
131+
// create libp2p
132+
const libp2p = await Libp2p.create(options)
133+
}
120134

121-
// create libp2p
122-
const libp2p = await Libp2p.create(options)
135+
main()
123136
```
124137

125138
Note: The [`PeerId`][peer-id] option is not required and will be generated if it is not provided.
@@ -131,12 +144,30 @@ As an alternative, it is possible to create a Libp2p instance with the construct
131144

132145
```js
133146
const Libp2p = require('libp2p')
147+
const TCP = require('libp2p-tcp')
148+
const MPLEX = require('libp2p-mplex')
149+
const { NOISE } = require('libp2p-noise')
150+
const PeerId = require('peer-id')
151+
152+
async function main () {
153+
const peerId = await PeerId.create();
154+
155+
// specify options
156+
// peerId is required when Libp2p is instantiated via the constructor
157+
const options = {
158+
peerId,
159+
modules: {
160+
transport: [TCP],
161+
streamMuxer: [MPLEX],
162+
connEncryption: [NOISE]
163+
}
164+
}
134165

135-
// specify options
136-
const options = {}
166+
// create libp2p
167+
const libp2p = new Libp2p(options)
168+
}
137169

138-
// create libp2p
139-
const libp2p = new Libp2p(options)
170+
main()
140171
```
141172

142173
Required keys in the `options` object:

examples/chat/src/dialer.js

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

44
const PeerId = require('peer-id')
55
const multiaddr = require('multiaddr')
6-
const Node = require('./libp2p-bundle')
6+
const createLibp2p = require('./libp2p-bundle')
77
const { stdinToStream, streamToConsole } = require('./stream')
88

99
async function run() {
@@ -13,7 +13,7 @@ async function run() {
1313
])
1414

1515
// Create a new libp2p node on localhost with a randomly chosen port
16-
const nodeDialer = new Node({
16+
const nodeDialer = await createLibp2p({
1717
peerId: idDialer,
1818
addresses: {
1919
listen: ['/ip4/0.0.0.0/tcp/0']

examples/chat/src/libp2p-bundle.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@ const { NOISE } = require('libp2p-noise')
77
const defaultsDeep = require('@nodeutils/defaults-deep')
88
const libp2p = require('../../..')
99

10-
class Node extends libp2p {
11-
constructor (_options) {
12-
const defaults = {
13-
modules: {
14-
transport: [
15-
TCP,
16-
WS
17-
],
18-
streamMuxer: [ mplex ],
19-
connEncryption: [ NOISE ]
20-
}
21-
}
22-
23-
super(defaultsDeep(_options, defaults))
10+
async function createLibp2p(_options) {
11+
const defaults = {
12+
modules: {
13+
transport: [TCP, WS],
14+
streamMuxer: [mplex],
15+
connEncryption: [NOISE],
16+
},
2417
}
18+
19+
return libp2p.create(defaultsDeep(_options, defaults))
2520
}
2621

27-
module.exports = Node
22+
module.exports = createLibp2p

examples/chat/src/listener.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
/* eslint-disable no-console */
33

44
const PeerId = require('peer-id')
5-
const Node = require('./libp2p-bundle.js')
5+
const createLibp2p = require('./libp2p-bundle.js')
66
const { stdinToStream, streamToConsole } = require('./stream')
77

88
async function run() {
99
// Create a new libp2p node with the given multi-address
1010
const idListener = await PeerId.createFromJSON(require('./peer-id-listener'))
11-
const nodeListener = new Node({
11+
const nodeListener = await createLibp2p({
1212
peerId: idListener,
1313
addresses: {
1414
listen: ['/ip4/0.0.0.0/tcp/10333']

examples/echo/src/dialer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* Dialer Node
66
*/
77

8-
const multiaddr = require('multiaddr')
98
const PeerId = require('peer-id')
10-
const Node = require('./libp2p-bundle')
9+
const createLibp2p = require('./libp2p-bundle')
1110
const pipe = require('it-pipe')
1211

1312
async function run() {
@@ -17,7 +16,7 @@ async function run() {
1716
])
1817

1918
// Dialer
20-
const dialerNode = new Node({
19+
const dialerNode = await createLibp2p({
2120
addresses: {
2221
listen: ['/ip4/0.0.0.0/tcp/0']
2322
},

examples/echo/src/libp2p-bundle.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,16 @@ const { NOISE } = require('libp2p-noise')
88
const defaultsDeep = require('@nodeutils/defaults-deep')
99
const libp2p = require('../../..')
1010

11-
class Node extends libp2p {
12-
constructor (_options) {
13-
const defaults = {
14-
modules: {
15-
transport: [
16-
TCP,
17-
WS
18-
],
19-
streamMuxer: [ mplex ],
20-
connEncryption: [ NOISE ]
21-
}
22-
}
23-
24-
super(defaultsDeep(_options, defaults))
11+
async function createLibp2p(_options) {
12+
const defaults = {
13+
modules: {
14+
transport: [TCP, WS],
15+
streamMuxer: [mplex],
16+
connEncryption: [NOISE],
17+
},
2518
}
19+
20+
return libp2p.create(defaultsDeep(_options, defaults))
2621
}
2722

28-
module.exports = Node
23+
module.exports = createLibp2p

examples/echo/src/listener.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
*/
77

88
const PeerId = require('peer-id')
9-
const Node = require('./libp2p-bundle')
9+
const createLibp2p = require('./libp2p-bundle')
1010
const pipe = require('it-pipe')
1111

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

1515
// Listener libp2p node
16-
const listenerNode = new Node({
16+
const listenerNode = await createLibp2p({
1717
addresses: {
1818
listen: ['/ip4/0.0.0.0/tcp/10333']
1919
},

0 commit comments

Comments
 (0)