Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.

Event names are noun:verb so the first part is the name of the object being acted on and the second is the action.

Type Parameters

Hierarchy

  • Libp2pEvents

Properties

connection:close: CustomEvent<Connection>

This event notifies listeners when incoming or outgoing connections are closed.

connection:open: CustomEvent<Connection>

This event notifies listeners when new incoming or outgoing connections are opened.

connection:prune: CustomEvent<Connection[]>

This event is dispatched when the connection manager has more than the configured allowable max connections and has closed some connections to bring the node back under the limit.

peer:connect: CustomEvent<PeerId>

This event will be triggered any time a new peer connects.

Example

libp2p.addEventListener('peer:connect', (event) => {
const peerId = event.detail
// ...
})
peer:disconnect: CustomEvent<PeerId>

This event will be triggered any time we are disconnected from another peer, regardless of the circumstances of that disconnection. If we happen to have multiple connections to a peer, this event will only be triggered when the last connection is closed.

Example

libp2p.addEventListener('peer:disconnect', (event) => {
const peerId = event.detail
// ...
})
peer:discovery: CustomEvent<PeerInfo>

This event is dispatched when a new network peer is discovered.

Example

libp2p.addEventListener('peer:discovery', (event) => {
const peerInfo = event.detail
// ...
})
peer:identify: CustomEvent<IdentifyResult>

This event is dispatched after a remote peer has successfully responded to the identify protocol. Note that for this to be emitted, both peers must have an identify service configured.

Example

libp2p.addEventListener('peer:identify', (event) => {
const identifyResult = event.detail
// ...
})
peer:update: CustomEvent<PeerUpdate>

This event is dispatched when the peer store data for a peer has been updated - e.g. their multiaddrs, protocols etc have changed.

If they were previously known to this node, the old peer data will be set in the previous field.

This may be in response to the identify protocol running, a manual update or some other event.

self:peer:update: CustomEvent<PeerUpdate>

This event is dispatched when the current node's peer record changes - for example a transport started listening on a new address or a new protocol handler was registered.

Example

libp2p.addEventListener('self:peer:update', (event) => {
const { peer } = event.detail
// ...
})
start: CustomEvent<Libp2p<T>>

This event notifies listeners that the node has started

libp2p.addEventListener('start', (event) => {
console.info(libp2p.isStarted()) // true
})

This event notifies listeners that the node has stopped

libp2p.addEventListener('stop', (event) => {
console.info(libp2p.isStarted()) // false
})
transport:close: CustomEvent<Listener>

This event is dispatched when a transport stops listening on an address

transport:listening: CustomEvent<Listener>

This event is dispatched when a transport begins listening on a new address