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.
A libp2p topology with a group of common peers.
A libp2p topology with a group of peers that support the same protocol.
$ npm install libp2p-interfaces
TBA
const Topology = require('libp2p-interfaces/src/topology')
const toplogy = new Topology({
min: 0,
max: 50
})
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')
const toplogy = new MulticodecTopology({
min: 0,
max: 50,
multicodecs: ['/echo/1.0.0'],
handlers: {
onConnect: (peerId, conn) => {},
onDisconnect: (peerId) => {}
}
})
The MulticodecTopology
extends the Topology
, which makes the Topology
API a subset of the MulticodecTopology
API.
Topology
peers<Map<string, PeerId>>
: A Map of peers belonging to the topology.disconnect<function(PeerId)>
: Called when a peer has been disconnected
const toplogy = new Topology({
min: 0,
max: 50,
handlers: {
onConnect: (peerId, conn) => {},
onDisconnect: (peerId) => {}
}
})
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.
topology.peers.set(id, peerId)
Add a peer to the topology.
Parameters
id
is thestring
that identifies the peer to add.peerId
is the PeerId of the peer to add.
topology.disconnect(peerId)
Parameters
peerId
is the PeerId of the peer disconnected.
MulticodecTopology
registrar<Registrar>
: TheRegistrar
of the topology. This is set by theRegistrar
during registration.peers<Map<string, PeerId>>
: The Map of peers that belong to the topologydisconnect<function(PeerId)>
: Disconnects a peer from the topology.
const toplogy = new MulticodecTopology({
min: 0,
max: 50,
multicodecs: ['/echo/1.0.0'],
handlers: {
onConnect: (peerId, conn) => {},
onDisconnect: (peerId) => {}
}
})
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.