From 494697470f215f78cbc1637136e4eb6da159ddd0 Mon Sep 17 00:00:00 2001 From: Mike Goelzer Date: Sun, 9 Sep 2018 06:13:53 -0700 Subject: [PATCH] Demonstrates floodsub in JS (non-working with go prog) --- js-dht-test/index.js | 46 +++++++++++++++++++++++++++++++++++++--- js-dht-test/package.json | 3 ++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/js-dht-test/index.js b/js-dht-test/index.js index 15cf99b..0ebea68 100644 --- a/js-dht-test/index.js +++ b/js-dht-test/index.js @@ -5,12 +5,15 @@ const TCP = require('libp2p-tcp') const Mplex = require('libp2p-mplex') const SECIO = require('libp2p-secio') const PeerInfo = require('peer-info') +const FloodSub = require('libp2p-floodsub') const CID = require('cids') const KadDHT = require('libp2p-kad-dht') const defaultsDeep = require('@nodeutils/defaults-deep') const waterfall = require('async/waterfall') const parallel = require('async/parallel') +var fsub; + class MyBundle extends libp2p { constructor (_options) { const defaults = { @@ -64,9 +67,35 @@ parallel([ (cb) => node1.dial(bootstrapAddr, cb), // Set up of the cons might take time (cb) => setTimeout(cb, 300) - ], (err) => { - if (err) { throw err } + ], (err) => { if (err) { throw err } + + + + console.log("My ID: " + node1.peerInfo.id._idB58String) + + fsub = new FloodSub(node1) + console.log("Initialized fsub\n") + fsub.start((err) => { + if (err) { + console.log('Error:', err) + } + fsub.on('libp2p-demo-chat', (data) => { + console.log(">>> Got some data:") + console.log(" From: " + data.from); + console.log(" Data: " + data.data); + console.log(" SeqNo: " + data.seqno); + console.log(" topicIDs: " + data.topicIDs); + console.log(">>> END - data\n") + }) + fsub.subscribe('libp2p-demo-chat') + + fsub.publish('libp2p-demo-chat', new Buffer('Hello from JS (0)')) + }) + + + + /* const provideCid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL') const findCid = new CID('zb2rhXqLbdjpXnJG99QsjM6Nc6xaDKgEr2FfugDJynE7H2NR6') @@ -91,6 +120,17 @@ parallel([ } }) }, 5000) - }) + })*/ + }) }) + + +var i = 0 +function pubsubloop() { + i = i + 1 + var s = new Buffer('Hello from JS ('+i+')') + fsub.publish('libp2p-demo-chat',s) +} + +setInterval(pubsubloop,10000); \ No newline at end of file diff --git a/js-dht-test/package.json b/js-dht-test/package.json index eca2211..a648196 100644 --- a/js-dht-test/package.json +++ b/js-dht-test/package.json @@ -17,6 +17,7 @@ "libp2p-mplex": "^0.8.0", "libp2p-secio": "^0.10.0", "libp2p-tcp": "^0.12.1", - "peer-info": "^0.14.1" + "peer-info": "^0.14.1", + "libp2p-floodsub": "^0.15.0" } }