mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 18:42:15 +00:00
refactor: examples-pubsub (#504)
This commit is contained in:
parent
6445fda050
commit
b08d81cee7
@ -1,103 +1,51 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const libp2p = require('../../')
|
const Libp2p = require('../../')
|
||||||
const TCP = require('libp2p-tcp')
|
const TCP = require('libp2p-tcp')
|
||||||
const Mplex = require('libp2p-mplex')
|
const Mplex = require('libp2p-mplex')
|
||||||
const SECIO = require('libp2p-secio')
|
const SECIO = require('libp2p-secio')
|
||||||
const PeerInfo = require('peer-info')
|
const PeerInfo = require('peer-info')
|
||||||
const MulticastDNS = require('libp2p-mdns')
|
|
||||||
const Gossipsub = require('libp2p-gossipsub')
|
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 {
|
const createNode = async () => {
|
||||||
constructor (_options) {
|
const peerInfo = await PeerInfo.create()
|
||||||
const defaults = {
|
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||||
modules: {
|
|
||||||
transport: [ TCP ],
|
const node = await Libp2p.create({
|
||||||
streamMuxer: [ Mplex ],
|
peerInfo,
|
||||||
connEncryption: [ SECIO ],
|
modules: {
|
||||||
peerDiscovery: [ MulticastDNS ],
|
transport: [TCP],
|
||||||
pubsub: Gossipsub
|
streamMuxer: [Mplex],
|
||||||
},
|
connEncryption: [SECIO],
|
||||||
config: {
|
pubsub: Gossipsub
|
||||||
peerDiscovery: {
|
|
||||||
mdns: {
|
|
||||||
interval: 2000,
|
|
||||||
enabled: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
pubsub: {
|
|
||||||
enabled: true,
|
|
||||||
emitSelf: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
super(defaultsDeep(_options, defaults))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createNode (callback) {
|
|
||||||
let node
|
|
||||||
|
|
||||||
waterfall([
|
|
||||||
(cb) => PeerInfo.create(cb),
|
|
||||||
(peerInfo, cb) => {
|
|
||||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
|
||||||
node = new MyBundle({
|
|
||||||
peerInfo
|
|
||||||
})
|
|
||||||
node.start(cb)
|
|
||||||
}
|
|
||||||
], (err) => callback(err, node))
|
|
||||||
}
|
|
||||||
|
|
||||||
parallel([
|
|
||||||
(cb) => createNode(cb),
|
|
||||||
(cb) => createNode(cb)
|
|
||||||
], (err, nodes) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
const node1 = nodes[0]
|
|
||||||
const node2 = nodes[1]
|
|
||||||
|
|
||||||
node1.once('peer:connect', (peer) => {
|
|
||||||
console.log('connected to %s', peer.id.toB58String())
|
|
||||||
|
|
||||||
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(() => {
|
|
||||||
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 }
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
await node.start()
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
const topic = 'news'
|
||||||
|
|
||||||
|
const [node1, node2] = await Promise.all([
|
||||||
|
createNode(),
|
||||||
|
createNode(),
|
||||||
|
])
|
||||||
|
|
||||||
|
await node1.dial(node2.peerInfo)
|
||||||
|
|
||||||
|
await node1.pubsub.subscribe(topic, (msg) => {
|
||||||
|
console.log(`node1 received: ${msg.data.toString()}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
await node2.pubsub.subscribe(topic, (msg) => {
|
||||||
|
console.log(`node2 received: ${msg.data.toString()}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// node2 publishes "news" every second
|
||||||
|
setInterval(() => {
|
||||||
|
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
|
||||||
|
}, 1000)
|
||||||
|
})();
|
||||||
|
@ -14,44 +14,47 @@ For this example, we will use MulticastDNS for automatic Peer Discovery. This ex
|
|||||||
|
|
||||||
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.
|
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
|
First, let's update our libp2p configuration with a pubsub implementation.
|
||||||
node1.once('peer:connect', (peer) => {
|
|
||||||
console.log('connected to %s', peer.id.toB58String())
|
|
||||||
|
|
||||||
series([
|
```JavaScript
|
||||||
// node1 subscribes to "news"
|
const Libp2p = require('libp2p')
|
||||||
(cb) => node1.pubsub.subscribe(
|
const Gossipsub = require('libp2p-gossipsub')
|
||||||
'news',
|
|
||||||
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
|
const node = await Libp2p.create({
|
||||||
cb
|
modules: {
|
||||||
),
|
transport: [ TCP ],
|
||||||
(cb) => setTimeout(cb, 500),
|
streamMuxer: [ Mplex ],
|
||||||
// node2 subscribes to "news"
|
connEncryption: [ SECIO ],
|
||||||
(cb) => node2.pubsub.subscribe(
|
// we add the Pubsub module we want
|
||||||
'news',
|
pubsub: Gossipsub
|
||||||
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
|
}
|
||||||
cb
|
|
||||||
),
|
|
||||||
(cb) => setTimeout(cb, 500),
|
|
||||||
// node2 publishes "news" every second
|
|
||||||
(cb) => {
|
|
||||||
setInterval(() => {
|
|
||||||
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 }
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Once that is done, we only need to create a few libp2p nodes, connect them and everything is ready to start using pubsub.
|
||||||
|
|
||||||
|
```JavaScript
|
||||||
|
const topic = 'news'
|
||||||
|
|
||||||
|
const node1 = nodes[0]
|
||||||
|
const node2 = nodes[1]
|
||||||
|
|
||||||
|
await node1.dial(node2.peerInfo)
|
||||||
|
|
||||||
|
await node1.pubsub.subscribe(topic, (msg) => {
|
||||||
|
console.log(`node1 received: ${msg.data.toString()}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
await node2.pubsub.subscribe(topic, (msg) => {
|
||||||
|
console.log(`node2 received: ${msg.data.toString()}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// node2 publishes "news" every second
|
||||||
|
setInterval(() => {
|
||||||
|
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
|
||||||
|
}, 1000)
|
||||||
|
```
|
||||||
|
|
||||||
The output of the program should look like:
|
The output of the program should look like:
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -68,12 +71,6 @@ You can change the pubsub `emitSelf` option if you don't want the publishing nod
|
|||||||
```JavaScript
|
```JavaScript
|
||||||
const defaults = {
|
const defaults = {
|
||||||
config: {
|
config: {
|
||||||
peerDiscovery: {
|
|
||||||
mdns: {
|
|
||||||
interval: 2000,
|
|
||||||
enabled: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
pubsub: {
|
pubsub: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
emitSelf: false
|
emitSelf: false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user