Skip to main content

Manage Peers


Before you can create add and retrieve peers, you will need to:

Add Peers

You can add peers using the SingleNodeClient.peerAdd(multiAddress, alias?) function. You will need to provide a multiAddress and, optionally, and alias.

Retrieve Peers

You can retrieve a SingleNodeClient's peers using the SingleNodeClient.peers() function. The peers() function will return a list of peers.

Retrieve Peer Data

Once you have retrieved your node's peers, you can use any of its properties to retrieve the peer's data.

Delete Peers

You can delete a SingleNodeClient's peer using the SingleNodeClient.peerDelete(peerId) function. You will need to provide a valid peerId.

Example Code

import { SingleNodeClient } from "@iota/iota.js";

const API_ENDPOINT = "http://localhost:14265/";

async function run() {
const client = new SingleNodeClient(API_ENDPOINT);

const peers = await client.peers();

if (peers) {
for (const peer of peers) {
console.log("\tMulti Address:", peer.multiAddresses);
console.log("\tAlias:", peer.alias);
console.log("\tRelation:", peer.relation);
console.log("\tConnected:", peer.connected);
if (peer.gossip) {
if (peer.gossip.heartbeat) {
console.log("\tGossip Heartbeat");
console.log("\t\tLatest Milestone Index:", peer.gossip.heartbeat.latestMilestoneIndex);
console.log("\t\tPruned Milestone Index:", peer.gossip.heartbeat.prunedMilestoneIndex);
console.log("\t\tSolid Milestone Index:", peer.gossip.heartbeat.solidMilestoneIndex);
console.log("\t\tConnected Peers:", peer.gossip.heartbeat.connectedPeers);
console.log("\t\tSynced Peers:", peer.gossip.heartbeat.syncedPeers);
console.log("\tGossip Metrics");
console.log("\t\tNew Blocks:", peer.gossip.metrics.newBlocks);
console.log("\t\tKnown Blocks:", peer.gossip.metrics.knownBlocks);
console.log("\t\tReceived Blocks:", peer.gossip.metrics.receivedBlocks);
console.log("\t\tReceived Block Requests:", peer.gossip.metrics.receivedBlockRequests);
console.log("\t\tReceived Milestone Requests:", peer.gossip.metrics.receivedMilestoneRequests);
console.log("\t\tReceived Hearbeats:", peer.gossip.metrics.receivedHeartbeats);
console.log("\t\tSent Blocks:", peer.gossip.metrics.sentBlocks);
console.log("\t\tSent Block Requests:", peer.gossip.metrics.sentBlockRequests);
console.log("\t\tSent Milestone Requests:", peer.gossip.metrics.sentMilestoneRequests);
console.log("\t\tSent Heartbeats:", peer.gossip.metrics.sentHeartbeats);
console.log("\t\tDropped Packets:", peer.gossip.metrics.droppedPackets);
} else {
console.log("\nNo Peers");

const address = "/ip4/";
const peerId = "djhgkdjfghkdfjhdfkjghkdfgh";
const addedPeer = await client.peerAdd(`${address}/p2p/${peerId}`, "fred");
console.log("Added Peer");

const peer2 = await client.peer(;
console.log("Got Peer",;

console.log("Deleted Peer");
await client.peerDelete(;

.then(() => console.log("Done"))
.catch(err => console.error(err));

Expected Output

Id: 12D3KooWNSCD7hha6BTPw44XeeKXcrAPBUGnPAY55gvXPen6zEAk
Multi Address: ['/ip4/']
Alias: undefined
Relation: autopeered
Connected: true
Gossip Heartbeat
Latest Milestone Index: 71452
Pruned Milestone Index: 55600
Solid Milestone Index: 71452
Connected Peers: 3
Synced Peers: 3
Gossip Metrics
New Blocks: 4282
Known Blocks: 6
Received Blocks: 4291
Received Block Requests: 0
Received Milestone Requests: 1
Received Hearbeats: 241
Sent Blocks: 22
Sent Block Requests: 6
Sent Milestone Requests: 1
Sent Heartbeats: 241
Dropped Packets: 0