Skip to content

Commit b0f124b

Browse files
a1300jacobheun
authored andcommitted
fix: pubsub configuration (#404)
* fix: add pubsub default config (#401) License: MIT Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net> * docs: add default pubsub config to README (#401) License: MIT Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net> * fix: pass config to provided PubSub (#401) License: MIT Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net> * docs: adapt pubsub/example for new config (#401) License: MIT Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net> * Update examples/pubsub/README.md Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * test: add pubsub config tests (#401) License: MIT Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net>
1 parent b294301 commit b0f124b

File tree

8 files changed

+169
-31
lines changed

8 files changed

+169
-31
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ class Node extends Libp2p {
192192
}
193193
},
194194
pubsub: {
195-
enabled: true
195+
enabled: true,
196+
emitSelf: true, // whether the node should emit to self on publish, in the event of the topic being subscribed
197+
signMessages: true, // if messages should be signed
198+
strictSigning: true // if message signing should be required
196199
}
197200
}
198201
}

examples/pubsub/1.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Gossipsub = require('libp2p-gossipsub')
1111
const defaultsDeep = require('@nodeutils/defaults-deep')
1212
const waterfall = require('async/waterfall')
1313
const parallel = require('async/parallel')
14+
const series = require('async/series')
1415

1516
class MyBundle extends libp2p {
1617
constructor (_options) {
@@ -28,6 +29,10 @@ class MyBundle extends libp2p {
2829
interval: 2000,
2930
enabled: true
3031
}
32+
},
33+
pubsub: {
34+
enabled: true,
35+
emitSelf: true
3136
}
3237
}
3338
}
@@ -63,19 +68,36 @@ parallel([
6368
node1.once('peer:connect', (peer) => {
6469
console.log('connected to %s', peer.id.toB58String())
6570

66-
// Subscribe to the topic 'news'
67-
node1.pubsub.subscribe('news',
68-
(msg) => console.log(msg.from, msg.data.toString()),
69-
() => {
71+
series([
72+
// node1 subscribes to "news"
73+
(cb) => node1.pubsub.subscribe(
74+
'news',
75+
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
76+
cb
77+
),
78+
(cb) => setTimeout(cb, 500),
79+
// node2 subscribes to "news"
80+
(cb) => node2.pubsub.subscribe(
81+
'news',
82+
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
83+
cb
84+
),
85+
(cb) => setTimeout(cb, 500),
86+
// node2 publishes "news" every second
87+
(cb) => {
7088
setInterval(() => {
71-
// Publish the message on topic 'news'
7289
node2.pubsub.publish(
7390
'news',
7491
Buffer.from('Bird bird bird, bird is the word!'),
75-
() => {}
92+
(err) => {
93+
if (err) { throw err }
94+
}
7695
)
7796
}, 1000)
78-
}
79-
)
97+
cb()
98+
},
99+
], (err) => {
100+
if (err) { throw err }
101+
})
80102
})
81103
})

examples/pubsub/README.md

+50-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Publish Subscribe
22

3-
Publish Subscribe is also included on the stack. Currently, we have on PubSub implementation which we ship by default [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub), with many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub).
3+
Publish Subscribe is also included on the stack. Currently, we have two PubSub implementation available [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub) and [libp2p-gossipsub](https://github.com/ChainSafe/gossipsub-js), with many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub).
44

55
We've seen many interesting use cases appear with this, here are some highlights:
66

@@ -12,26 +12,43 @@ We've seen many interesting use cases appear with this, here are some highlights
1212

1313
For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js).
1414

15-
Using PubSub is super simple, all you have to do is start a libp2p node with `EXPERIMENTAL.pubsub` set to true.
15+
Using PubSub is super simple, you only need to provide the implementation of your choice and you are ready to go. No need for extra configuration.
1616

1717
```JavaScript
1818
node1.once('peer:connect', (peer) => {
1919
console.log('connected to %s', peer.id.toB58String())
2020

21-
// Subscribe to the topic 'news'
22-
node1.pubsub.subscribe('news',
23-
(msg) => console.log(msg.from, msg.data.toString()),
24-
() => {
21+
series([
22+
// node1 subscribes to "news"
23+
(cb) => node1.pubsub.subscribe(
24+
'news',
25+
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
26+
cb
27+
),
28+
(cb) => setTimeout(cb, 500),
29+
// node2 subscribes to "news"
30+
(cb) => node2.pubsub.subscribe(
31+
'news',
32+
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
33+
cb
34+
),
35+
(cb) => setTimeout(cb, 500),
36+
// node2 publishes "news" every second
37+
(cb) => {
2538
setInterval(() => {
26-
// Publish the message on topic 'news'
2739
node2.pubsub.publish(
2840
'news',
2941
Buffer.from('Bird bird bird, bird is the word!'),
30-
() => {}
42+
(err) => {
43+
if (err) { throw err }
44+
}
3145
)
3246
}, 1000)
33-
}
34-
)
47+
cb()
48+
},
49+
], (err) => {
50+
if (err) { throw err }
51+
})
3552
})
3653
```
3754

@@ -40,11 +57,29 @@ The output of the program should look like:
4057
```
4158
> node 1.js
4259
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
43-
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
44-
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
45-
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
46-
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
47-
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
60+
node2 received: Bird bird bird, bird is the word!
61+
node1 received: Bird bird bird, bird is the word!
62+
node2 received: Bird bird bird, bird is the word!
63+
node1 received: Bird bird bird, bird is the word!
64+
```
65+
66+
You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages.
67+
68+
```JavaScript
69+
const defaults = {
70+
config: {
71+
peerDiscovery: {
72+
mdns: {
73+
interval: 2000,
74+
enabled: true
75+
}
76+
},
77+
pubsub: {
78+
enabled: true,
79+
emitSelf: false
80+
}
81+
}
82+
}
4883
```
4984

5085
## 2. Future work

src/config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ const configSchema = s({
6363
// Pubsub config
6464
pubsub: s('object?', {
6565
// Pubsub defaults
66-
enabled: true
66+
enabled: true,
67+
emitSelf: true,
68+
signMessages: true,
69+
strictSigning: true
6770
})
6871
}, {})
6972

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Libp2p extends EventEmitter {
124124

125125
// start pubsub
126126
if (this._modules.pubsub && this._config.pubsub.enabled !== false) {
127-
this.pubsub = pubsub(this, this._modules.pubsub)
127+
this.pubsub = pubsub(this, this._modules.pubsub, this._config.pubsub)
128128
}
129129

130130
// Attach remaining APIs

src/pubsub.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const promisify = require('promisify-es6')
66

77
const errCode = require('err-code')
88

9-
module.exports = (node, Pubsub) => {
10-
const pubsub = new Pubsub(node, { emitSelf: true })
9+
module.exports = (node, Pubsub, config) => {
10+
const pubsub = new Pubsub(node, config)
1111

1212
return {
1313
/**

test/config.spec.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ describe('configuration', () => {
8383
autoDial: true
8484
},
8585
pubsub: {
86-
enabled: true
86+
enabled: true,
87+
emitSelf: true,
88+
signMessages: true,
89+
strictSigning: true
8790
},
8891
dht: {
8992
kBucketSize: 20,
@@ -145,7 +148,10 @@ describe('configuration', () => {
145148
}
146149
},
147150
pubsub: {
148-
enabled: true
151+
enabled: true,
152+
emitSelf: true,
153+
signMessages: true,
154+
strictSigning: true
149155
},
150156
dht: {
151157
kBucketSize: 20,
@@ -270,7 +276,10 @@ describe('configuration', () => {
270276
},
271277
config: {
272278
pubsub: {
273-
enabled: true
279+
enabled: true,
280+
emitSelf: true,
281+
signMessages: true,
282+
strictSigning: true
274283
},
275284
peerDiscovery: {
276285
autoDial: true

test/pubsub.node.js

+66
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,70 @@ describe('.pubsub', () => {
367367
})
368368
})
369369
})
370+
371+
describe('.pubsub config', () => {
372+
it('toggle all pubsub options off (except enabled)', done => {
373+
expect(3).checks(done)
374+
375+
class PubSubSpy {
376+
constructor (node, config) {
377+
expect(config).to.be.eql({
378+
enabled: true,
379+
selfEmit: false,
380+
signMessages: false,
381+
strictSigning: false
382+
}).mark()
383+
}
384+
}
385+
386+
createNode('/ip4/0.0.0.0/tcp/0', {
387+
modules: {
388+
pubsub: PubSubSpy
389+
},
390+
config: {
391+
pubsub: {
392+
enabled: true,
393+
selfEmit: false,
394+
signMessages: false,
395+
strictSigning: false
396+
}
397+
}
398+
}, (err, node) => {
399+
expect(err).to.not.exist().mark()
400+
expect(node).to.exist().mark()
401+
})
402+
})
403+
404+
it('toggle all pubsub options on', done => {
405+
expect(3).checks(done)
406+
407+
class PubSubSpy {
408+
constructor (node, config) {
409+
expect(config).to.be.eql({
410+
enabled: true,
411+
selfEmit: true,
412+
signMessages: true,
413+
strictSigning: true
414+
}).mark()
415+
}
416+
}
417+
418+
createNode('/ip4/0.0.0.0/tcp/0', {
419+
modules: {
420+
pubsub: PubSubSpy
421+
},
422+
config: {
423+
pubsub: {
424+
enabled: true,
425+
selfEmit: true,
426+
signMessages: true,
427+
strictSigning: true
428+
}
429+
}
430+
}, (err, node) => {
431+
expect(err).to.not.exist().mark()
432+
expect(node).to.exist().mark()
433+
})
434+
})
435+
})
370436
})

0 commit comments

Comments
 (0)