From 8fcafe2d901e594cbf9d0b258b4c0f9b1476d80e Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 15 Feb 2018 19:49:41 +0100 Subject: [PATCH] docs: update pubsub example --- examples/pubsub/1.js | 16 ++++++---------- examples/pubsub/README.md | 21 ++++++--------------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/examples/pubsub/1.js b/examples/pubsub/1.js index c2e8436b..e522088d 100644 --- a/examples/pubsub/1.js +++ b/examples/pubsub/1.js @@ -6,7 +6,6 @@ const Multiplex = require('libp2p-multiplex') const SECIO = require('libp2p-secio') const PeerInfo = require('peer-info') const MulticastDNS = require('libp2p-mdns') -const FloodSub = require('libp2p-floodsub') const waterfall = require('async/waterfall') const parallel = require('async/parallel') const series = require('async/series') @@ -19,7 +18,9 @@ class MyBundle extends libp2p { muxer: [Multiplex], crypto: [SECIO] }, - discovery: [new MulticastDNS(peerInfo, { interval: 2000 })] + discovery: [ + new MulticastDNS(peerInfo, { interval: 2000 }) + ] } super(modules, peerInfo) } @@ -47,22 +48,17 @@ parallel([ const node1 = nodes[0] const node2 = nodes[1] - const fs1 = new FloodSub(node1) - const fs2 = new FloodSub(node2) - series([ - (cb) => fs1.start(cb), - (cb) => fs2.start(cb), (cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)), (cb) => setTimeout(cb, 500) ], (err) => { if (err) { throw err } - fs2.on('news', (msg) => console.log(msg.from, msg.data.toString())) - fs2.subscribe('news') + node1.pubsub.on('news', (msg) => console.log(msg.from, msg.data.toString())) + node1.pubsub.subscribe('news') setInterval(() => { - fs1.publish('news', Buffer.from('Bird bird bird, bird is the word!')) + node2.pubsub.publish('news', Buffer.from('Bird bird bird, bird is the word!')) }, 1000) }) }) diff --git a/examples/pubsub/README.md b/examples/pubsub/README.md index f84ffbcd..6279f2b4 100644 --- a/examples/pubsub/README.md +++ b/examples/pubsub/README.md @@ -1,8 +1,6 @@ # Publish Subscribe -Publish Subscribe is something out of scope for the modular networking stack that is libp2p, however, it is something that is enabled through the primitives that libp2p offers and so it has become one of the most interesting use cases for libp2p. - -Currently, we have a PubSub implementation, [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub) and many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub). +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). We've seen many interesting use cases appear with this, here are some highlights: @@ -12,29 +10,22 @@ We've seen many interesting use cases appear with this, here are some highlights ## 1. Setting up a simple PubSub network on top of libp2p -For this example, we will use MulticastDNS for automatic Peer Discovery and libp2p-floodsub to give us the PubSub primitives we are looking for. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js). +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). -Getting PubSub is super simple, all you have to do is require the FloodSub module and pass it in a libp2p node, once you have done that you can start subscribing and publishing in any topic. +Using PubSub is super simple, all you have to do is start a libp2p node, PubSub will be enabled by default. ```JavaScript -const FloodSub = require('libp2p-floodsub') - -const fs1 = new FloodSub(node1) -const fs2 = new FloodSub(node2) - series([ - (cb) => fs1.start(cb), - (cb) => fs2.start(cb), (cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)), (cb) => setTimeout(cb, 500) ], (err) => { if (err) { throw err } - fs2.on('news', (msg) => console.log(msg.from, msg.data.toString())) - fs2.subscribe('news') + node1.pubsub.on('news', (msg) => console.log(msg.from, msg.data.toString())) + node1.pubsub.subscribe('news') setInterval(() => { - fs1.publish('news', Buffer.from('Bird bird bird, bird is the word!')) + node2.pubsub.publish('news', Buffer.from('Bird bird bird, bird is the word!')) }, 1000) }) ```