Skip to content

Instantly share code, notes, and snippets.

@Pierozi Pierozi/listener.js
Created Aug 22, 2019

Embed
What would you like to do?
LibP2P Listener Circuit Relay
'use strict'
/* eslint-disable no-console */
const relayId = "QmemquW1rBrxZ4nq1KvNSKgHiRqKhXQdcW4ZvWSMftVAuy"
const relayPort = "54783"
const Node = require('./node-circuit')
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const PeerId = require('peer-id')
const peerBook = new PeerBook()
const relayPeerId = PeerId.createFromB58String(relayId)
const relayPeerInfo = new PeerInfo(relayPeerId)
relayPeerInfo.multiaddrs.add(`/ip4/127.0.0.1/tcp/${relayPort}`)
peerBook.put(relayPeerInfo)
PeerInfo.create((err, peerInfo) => {
console.log('Peer ID: ', peerInfo.id.toB58String())
const listenerNode = new Node({
peerInfo: peerInfo,
peerBook: peerBook,
});
listenerNode.on('peer:connect', (peerInfo) => {
console.log('received dial to me from:', peerInfo.id.toB58String());
});
listenerNode.handle('/echo/1.0.0', (protocol, conn) => {
console.log('callback handle');
pull(
conn,
pull.map((data) => {
return data.toString('utf8').replace('\n', '')
}),
pull.drain(console.log)
)
});
listenerNode.start(() => {
console.log('callback start')
});
console.log('Listener ready, listening on:');
listenerNode.peerInfo.multiaddrs.forEach((ma) => {
console.log(ma.toString())
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.