Last active
September 24, 2020 17:33
-
-
Save vasco-santos/8a8b3377af15cfebad75a033aa84c995 to your computer and use it in GitHub Desktop.
libp2p protocol setup
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 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