Created
July 14, 2019 01:12
-
-
Save joeykrug/3b8d86e30813ea8fc4a947b64188010a to your computer and use it in GitHub Desktop.
index file for pubsub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint no-console: ["error", { allow: ["log"] }] */ | |
/* eslint max-nested-callbacks: ["error", 5] */ | |
'use strict' | |
// would publish every 24 hr and also publish on connection to a new peer | |
const domReady = require('detect-dom-ready') | |
const createNode = require('./create-node') | |
const FloodSub = require('libp2p-floodsub') | |
domReady(() => { | |
const myPeerDiv = document.getElementById('my-peer') | |
const swarmDiv = document.getElementById('swarm') | |
createNode((err, node) => { | |
if (err) { | |
return console.log('Could not create the Node, check if your browser has WebRTC Support', err) | |
} | |
node.on('peer:discovery', (peerInfo) => { | |
console.log('Discovered a peer:', peerInfo.id.toB58String()) | |
}) | |
node.on('peer:connect', (peerInfo) => { | |
const idStr = peerInfo.id.toB58String() | |
console.log('Got connection to: ' + idStr) | |
const connDiv = document.createElement('div') | |
connDiv.innerHTML = 'Connected to: ' + idStr | |
connDiv.id = idStr | |
swarmDiv.append(connDiv) | |
}) | |
node.on('peer:disconnect', (peerInfo) => { | |
const idStr = peerInfo.id.toB58String() | |
const el = document.getElementById(idStr) | |
el && el.remove() | |
}) | |
node.start((err) => { | |
if (err) { | |
return console.log(err) | |
} | |
const idStr = node.peerInfo.id.toB58String() | |
const idDiv = document | |
.createTextNode('Node is ready. ID: ' + idStr) | |
myPeerDiv.append(idDiv) | |
console.log('Node is listening o/') | |
// NOTE: to stop the node | |
// node.stop((err) => {}) | |
}) | |
const fsub = new FloodSub(node) | |
fsub.start((err) => { | |
console.log('pubSUBB') | |
if (err) { | |
console.log('Upsy', err) | |
} | |
fsub.on('fruit', (data) => { | |
console.log(data) | |
}) | |
fsub.subscribe('fruit') | |
fsub.publish('fruit', new Buffer('banana')) | |
}) | |
}) | |
createNode((err, node) => { | |
if (err) { | |
return console.log('Could not create the Node, check if your browser has WebRTC Support', err) | |
} | |
node.start((err) => { | |
if (err) { | |
return console.log(err) | |
} | |
const idStr = node.peerInfo.id.toB58String() | |
const idDiv = document | |
.createTextNode('Node is ready. ID: ' + idStr) | |
myPeerDiv.append(idDiv) | |
console.log('Node is listening o/') | |
// NOTE: to stop the node | |
// node.stop((err) => {}) | |
}) | |
const fsub = new FloodSub(node) | |
fsub.start((err) => { | |
console.log('pubSUBB') | |
if (err) { | |
console.log('Upsy', err) | |
} | |
fsub.on('fruit', (data) => { | |
console.log(data) | |
}) | |
fsub.subscribe('fruit') | |
fsub.publish('fruit', new Buffer('banana')) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment