Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Last active September 24, 2020 17:33
Show Gist options
  • Save vasco-santos/8a8b3377af15cfebad75a033aa84c995 to your computer and use it in GitHub Desktop.
Save vasco-santos/8a8b3377af15cfebad75a033aa84c995 to your computer and use it in GitHub Desktop.
libp2p protocol setup
const Libp2p = require('libp2p')
const Mplex = require('libp2p-mplex')
const KickoffProtocol = require('./kickoff-protocol')
const node = await Libp2p.create({
  modules: {
// … other required modules
  streamMuxer: [ Mplex ],
  }
})
node.handle(KickoffProtocol.PROTOCOL, KickoffProtocol.handler)
await node.start()
// Send message to other peers (connected) using the protocol
node.peerStore.peers.forEach(async (peer) => {
if (!peer.protocols.includes(KickoffProtocol.PROTOCOL)) return
const connection = node.connectionManager.get(peer.id)
if (!connection) return
try {
const { stream } = await connection.newStream([KickoffProtocol.PROTOCOL])
await KickoffProtocol.send(message, stream)
} catch (err) {
console.error('Could not negotiate kickoff protocol stream with peer', err)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment