Private
#privateOptional
afterOptional
afterOptional
beforeOptional
beforeThe 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: [] ... }
}
Dials 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.
const conn = await libp2p.dial(remotePeerId)
// create a new stream within the connection
const { stream, protocol } = 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()
Optional
options: AbortOptionsDials 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.
import { 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)
Optional
options: AbortOptionsReturn a list of all connections this node has open, optionally filtering by a PeerId
for (const connection of libp2p.getConnections()) {
console.log(peerId, connection.remoteAddr.toString())
// Logs the PeerId string and the observed remote multiaddr of each Connection
}
Optional
peerId: PeerIdReturn 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 a list of supported protocols
const protocols = libp2p.getProtocols()
// [ '/ipfs/ping/1.0.0', '/ipfs/id/1.0.0' ]
Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type this may mean searching the DHT if the key is not present in the KeyStore. A set of user defined services
Optional
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.
const handler = ({ connection, stream, protocol }) => {
// use stream or connection according to the needs
}
libp2p.handle('/echo/1.0.0', handler, {
maxInboundStreams: 5,
maxOutboundStreams: 5
})
Optional
options: StreamHandlerOptionsThe keychain contains the keys used by the current node, and can create new keys, export them, import them, etc.
const keyInfo = await libp2p.keychain.createKey('new key')
console.info(keyInfo)
// { id: '...', name: 'new key' }
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: [] ... }
Register a topology to be informed when peers are encountered that support the specified protocol
import { createTopology } from '@libp2p/topology'
const id = await libp2p.register('/echo/1.0.0', createTopology({
onConnect: (peer, connection) => {
// handle connect
},
onDisconnect: (peer, connection) => {
// handle disconnect
}
}))
A set of user defined services
Unregister topology to no longer be informed when peers connect or disconnect.
const id = await libp2p.register(...)
libp2p.unregister(id)
Optional
options: boolean | AddEventListenerOptionsOptional
listener: null | EventHandler<Libp2pEvents<T>[K]>Optional
options: boolean | EventListenerOptions
Libp2p nodes implement this interface.