The content routing subsystem allows the user to find providers for content, let the network know they are providers for content, and get/put values to the DHT.
for await (const peerInfo of libp2p.contentRouting.findProviders(cid)) {
console.info(peerInfo)
// { id: PeerId(12D3Foo...), multiaddrs: [] ... }
}
The logger used by this libp2p node
Optional
metricsThe metrics subsystem allows recording values to assess the health/performance of the running node.
const metric = libp2p.metrics.registerMetric({
'my-metric'
})
// later
metric.update(5)
The PeerId is a unique identifier for a node on the network.
It is the hash of an RSA public key or, for Ed25519 or secp256k1 keys, the key itself.
console.info(libp2p.peerId)
// PeerId(12D3Foo...)
The peer routing subsystem allows the user to find peers on the network or to find peers close to binary keys.
const peerInfo = await libp2p.peerRouting.findPeer(peerId)
console.info(peerInfo)
// { id: PeerId(12D3Foo...), multiaddrs: [] ... }
for await (const peerInfo of libp2p.peerRouting.getClosestPeers(key)) {
console.info(peerInfo)
// { id: PeerId(12D3Foo...), multiaddrs: [] ... }
}
The peer store holds information we know about other peers on the network.
const peer = await libp2p.peerStore.get(peerId)
console.info(peer)
// { id: PeerId(12D3Foo...), addresses: [] ... }
A set of user defined services
The current status of the libp2p node
Optional
options: boolean | AddEventListenerOptionsOptional
afterOptional
afterOptional
beforeOptional
beforeDials to the provided peer. If successful, the known metadata of the
peer will be added to the nodes peerStore
.
If a PeerId is passed as the first argument, the peer will need to have known multiaddrs for it in the PeerStore.
Optional
options: DialOptionsconst conn = await libp2p.dial(remotePeerId)
// create a new stream within the connection
const stream = await conn.newStream(['/echo/1.1.0', '/echo/1.0.0'])
// protocol negotiated: 'echo/1.0.0' means that the other party only supports the older version
// ...
await conn.close()
Dials to the provided peer and tries to handshake with the given protocols in order.
If successful, the known metadata of the peer will be added to the nodes peerStore
,
and the MuxedStream
will be returned together with the successful negotiated protocol.
Optional
options: DialProtocolOptionsimport { pipe } from 'it-pipe'
const { stream, protocol } = await libp2p.dialProtocol(remotePeerId, protocols)
// Use this new stream like any other duplex stream
pipe([1, 2, 3], stream, consume)
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
Return a list of all connections this node has open, optionally filtering by a PeerId
Optional
peerId: PeerIdfor (const connection of libp2p.getConnections()) {
console.log(peerId, connection.remoteAddr.toString())
// Logs the PeerId string and the observed remote multiaddr of each Connection
}
Return the list of dials currently in progress or queued to start
for (const pendingDial of libp2p.getDialQueue()) {
console.log(pendingDial)
}
Get a deduplicated list of peer advertising multiaddrs by concatenating the listen addresses used by transports with any configured announce addresses as well as observed addresses reported by peers.
If Announce addrs are specified, configured listen addresses will be ignored though observed addresses will still be included.
const listenMa = libp2p.getMultiaddrs()
// [ <Multiaddr 047f00000106f9ba - /ip4/127.0.0.1/tcp/63930> ]
Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type this may mean searching the routing if the peer's key is not present in the peer store.
Optional
options: AbortOptionsOptional
options: AbortOptionsOptional
options: AbortOptionsOptional
options: AbortOptionsOptional
options: AbortOptionsSets up multistream-select routing of protocols to their application handlers. Whenever a stream is opened on one of the provided protocols, the handler will be called. handle
must be called in order to register a handler and support for a given protocol. This also informs other peers of the protocols you support.
libp2p.handle(protocols, handler, options)
In the event of a new handler for the same protocol being added, the first one is discarded.
Optional
options: StreamHandlerOptionsconst handler = ({ connection, stream, protocol }) => {
// use stream or connection according to the needs
}
libp2p.handle('/echo/1.0.0', handler, {
maxInboundStreams: 5,
maxOutboundStreams: 5
})
Attempts to gracefully close an open connection to the given peer. If the connection is not closed in the grace period, it will be forcefully closed.
An AbortSignal can optionally be passed to control when the connection is forcefully closed.
Optional
options: AbortOptionsawait libp2p.hangUp(remotePeerId)
Given the current node configuration, returns a promise of true
or
false
if the node would attempt to dial the passed multiaddr.
This means a relevant transport is configured, and the connection gater would not block the dial attempt.
This may involve resolving DNS addresses so you should pass an AbortSignal.
Optional
options: IsDialableOptionsRegister a topology to be informed when peers are encountered that support the specified protocol
const id = await libp2p.register('/echo/1.0.0', {
onConnect: (peer, connection) => {
// handle connect
},
onDisconnect: (peer, connection) => {
// handle disconnect
}
})
Optional
listener: null | EventHandler<Libp2pEvents<T>[K]>Optional
options: boolean | EventListenerOptionsOptional
listener: EventHandler<Event>Optional
options: boolean | EventListenerOptions
Libp2p nodes implement this interface.