Created
May 25, 2018 14:54
-
-
Save anku94/625df45f91e36de1ebbf69b4cf33a70f to your computer and use it in GitHub Desktop.
Libp2p discovery using webrtc-star
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
const libp2p = require('libp2p') | |
const TCP = require('libp2p-tcp') | |
const Mplex = require('libp2p-mplex') | |
const SECIO = require('libp2p-secio') | |
const PeerInfo = require('peer-info') | |
const Railing = require('libp2p-railing') | |
const waterfall = require('async/waterfall') | |
const wrtc = require('wrtc'); | |
const wstar = require('libp2p-webrtc-star') | |
const ws1 = new wstar({wrtc: wrtc}); | |
class MyBundle extends libp2p { | |
constructor (peerInfo) { | |
const modules = { | |
transport: [ws1], | |
connection: { | |
muxer: [Mplex], | |
crypto: [SECIO] | |
}, | |
discovery: [ws1.discovery] | |
} | |
super(modules, peerInfo) | |
} | |
} | |
waterfall([ | |
(cb) => PeerInfo.create(cb), | |
(peerInfo, cb) => { | |
const peerIdStr = peerInfo.id.toB58String(); | |
const ma = '/dns4/star-signal.cloud.ipfs.team/tcp/443/wss/p2p-webrtc-star/ipfs/' + peerIdStr; | |
//const ma = '/ip4/127.0.0.1/tcp/9090/ws/p2p-webrtc-star/ipfs/' + peerIdStr; | |
// peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') | |
peerInfo.multiaddrs.add(ma); | |
node = new MyBundle(peerInfo) | |
node.start(cb) | |
} | |
], (err) => { | |
if (err) { throw err } | |
node.on('peer:discovery', (peer) => { | |
if (!node.peerBook.has(peer)) { | |
console.log('Discovered:', peer.id.toB58String()) | |
let ourID = node.peerInfo.id.toB58String(); | |
console.log("Our ID: ", ourID); | |
node.peerBook.put(peer); | |
node.dial(peer, () => {}) | |
} | |
}) | |
node.on('peer:connect', (peer) => { | |
console.log('Connection established to:', peer.id.toB58String()) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment