mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-22 13:31:32 +00:00
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>
This commit is contained in:
@ -11,6 +11,7 @@ const Gossipsub = require('libp2p-gossipsub')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const series = require('async/series')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
@ -28,6 +29,10 @@ class MyBundle extends libp2p {
|
||||
interval: 2000,
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -63,19 +68,36 @@ parallel([
|
||||
node1.once('peer:connect', (peer) => {
|
||||
console.log('connected to %s', peer.id.toB58String())
|
||||
|
||||
// Subscribe to the topic 'news'
|
||||
node1.pubsub.subscribe('news',
|
||||
(msg) => console.log(msg.from, msg.data.toString()),
|
||||
() => {
|
||||
series([
|
||||
// node1 subscribes to "news"
|
||||
(cb) => node1.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 subscribes to "news"
|
||||
(cb) => node2.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 publishes "news" every second
|
||||
(cb) => {
|
||||
setInterval(() => {
|
||||
// Publish the message on topic 'news'
|
||||
node2.pubsub.publish(
|
||||
'news',
|
||||
Buffer.from('Bird bird bird, bird is the word!'),
|
||||
() => {}
|
||||
(err) => {
|
||||
if (err) { throw err }
|
||||
}
|
||||
)
|
||||
}, 1000)
|
||||
}
|
||||
)
|
||||
cb()
|
||||
},
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Publish Subscribe
|
||||
|
||||
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).
|
||||
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).
|
||||
|
||||
We've seen many interesting use cases appear with this, here are some highlights:
|
||||
|
||||
@ -12,26 +12,43 @@ We've seen many interesting use cases appear with this, here are some highlights
|
||||
|
||||
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).
|
||||
|
||||
Using PubSub is super simple, all you have to do is start a libp2p node with `EXPERIMENTAL.pubsub` set to true.
|
||||
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.
|
||||
|
||||
```JavaScript
|
||||
node1.once('peer:connect', (peer) => {
|
||||
console.log('connected to %s', peer.id.toB58String())
|
||||
|
||||
// Subscribe to the topic 'news'
|
||||
node1.pubsub.subscribe('news',
|
||||
(msg) => console.log(msg.from, msg.data.toString()),
|
||||
() => {
|
||||
series([
|
||||
// node1 subscribes to "news"
|
||||
(cb) => node1.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 subscribes to "news"
|
||||
(cb) => node2.pubsub.subscribe(
|
||||
'news',
|
||||
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
|
||||
cb
|
||||
),
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// node2 publishes "news" every second
|
||||
(cb) => {
|
||||
setInterval(() => {
|
||||
// Publish the message on topic 'news'
|
||||
node2.pubsub.publish(
|
||||
'news',
|
||||
Buffer.from('Bird bird bird, bird is the word!'),
|
||||
() => {}
|
||||
(err) => {
|
||||
if (err) { throw err }
|
||||
}
|
||||
)
|
||||
}, 1000)
|
||||
}
|
||||
)
|
||||
cb()
|
||||
},
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
@ -40,11 +57,29 @@ The output of the program should look like:
|
||||
```
|
||||
> node 1.js
|
||||
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
node2 received: Bird bird bird, bird is the word!
|
||||
node1 received: Bird bird bird, bird is the word!
|
||||
```
|
||||
|
||||
You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages.
|
||||
|
||||
```JavaScript
|
||||
const defaults = {
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 2000,
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true,
|
||||
emitSelf: false
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Future work
|
||||
|
Reference in New Issue
Block a user