mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-04-02 13:41:18 +00:00
* fix: remove use of assert module The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native. * chore: pr comments
interface-topology
Implementation of the topology interface used by the
js-libp2p
registrar.
Topologies can be used in conjunction with js-libp2p
to help shape its network and the overlays of its subsystems, such as pubsub and the DHT.
Table of Contents
Implementations
Topology
A libp2p topology with a group of common peers.
Multicodec Topology
A libp2p topology with a group of peers that support the same protocol.
Install
$ npm install libp2p-interfaces
Modules using the interface
TBA
Usage
Topology
const Topology = require('libp2p-interfaces/src/topology')
const toplogy = new Topology({
min: 0,
max: 50
})
Multicodec Topology
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')
const toplogy = new MulticodecTopology({
min: 0,
max: 50,
multicodecs: ['/echo/1.0.0'],
handlers: {
onConnect: (peerInfo, conn) => {},
onDisconnect: (peerInfo) => {}
}
})
API
The MulticodecTopology
extends the Topology
, which makes the Topology
API a subset of the MulticodecTopology
API.
Topology
Topology
peers<Map<string, PeerInfo>>
: A Map of peers belonging to the topology.disconnect<function(PeerInfo)>
: Called when a peer has been disconnected
Constructor
const toplogy = new Topology({
min: 0,
max: 50,
handlers: {
onConnect: (peerInfo, conn) => {},
onDisconnect: (peerInfo) => {}
}
})
Parameters
properties
is anObject
containing the properties of the topology.min
is anumber
with the minimum needed connections (default: 0)max
is anumber
with the maximum needed connections (default: Infinity)handlers
is an optionalObject
containing the handler called when a peer is connected or disconnected.onConnect
is afunction
called everytime a peer is connected in the topology context.onDisconnect
is afunction
called everytime a peer is disconnected in the topology context.
Set a peer
topology.peers.set(id, peerInfo)
Add a peer to the topology.
Parameters
id
is thestring
that identifies the peer to add.peerInfo
is the [PeerInfo][peer-info] of the peer to add.
Notify about a peer disconnected event
topology.disconnect(peerInfo)
Parameters
peerInfo
is the [PeerInfo][peer-info] of the peer disconnected.
Multicodec Topology
MulticodecTopology
registrar<Registrar>
: TheRegistrar
of the topology. This is set by theRegistrar
during registration.peers<Map<string, PeerInfo>>
: The Map of peers that belong to the topologydisconnect<function(PeerInfo)>
: Disconnects a peer from the topology.
Constructor
const toplogy = new MulticodecTopology({
min: 0,
max: 50,
multicodecs: ['/echo/1.0.0'],
handlers: {
onConnect: (peerInfo, conn) => {},
onDisconnect: (peerInfo) => {}
}
})
Parameters
properties
is anObject
containing the properties of the topology.min
is anumber
with the minimum needed connections (default: 0)max
is anumber
with the maximum needed connections (default: Infinity)multicodecs
is aArray<String>
with the multicodecs associated with the topology.handlers
is an optionalObject
containing the handler called when a peer is connected or disconnected.onConnect
is afunction
called everytime a peer is connected in the topology context.onDisconnect
is afunction
called everytime a peer is disconnected in the topology context.