⚠️Blast API (blastapi.io) ends Oct 31. Migrate to Dwellir and skip Alchemy's expensive compute units.
Switch Today →
Skip to main content

net_peerCount

The net_peerCount method returns the total number of network peers currently connected to your XDC node. This metric is crucial for understanding your node's level of network participation and its ability to synchronize blockchain data effectively across the XDC Network's distributed infrastructure.

A healthy peer count indicates strong network connectivity and ensures your node receives timely updates about new blocks, transactions, and network events. For XDC Network's XDPoS consensus mechanism, maintaining adequate peer connections is essential for proper validator communication and network consensus.

The peer count directly impacts your node's reliability, data synchronization speed, and overall contribution to network decentralization. Monitoring this metric helps identify network isolation issues, firewall problems, or configuration errors that might prevent optimal node performance.

Parameters

No parameters required. This method provides a simple count of active peer connections.

{
"jsonrpc": "2.0",
"method": "net_peerCount",
"params": [],
"id": 1
}

Returns

QUANTITY - Hexadecimal value representing the number of currently connected peers. Must be converted to decimal for human-readable format.

Use Cases

  1. Network Health Dashboards: Display real-time peer connectivity metrics in monitoring dashboards
  2. Connection Quality Assessment: Evaluate whether the node has sufficient peer connections for reliable operation
  3. Isolation Detection: Identify nodes that have become isolated from the network due to configuration issues
  4. Performance Optimization: Correlate peer count with sync speed to optimize node configuration
  5. Geographic Distribution Analysis: Monitor peer diversity for enhanced network resilience
  6. Automated Scaling: Trigger infrastructure scaling based on peer connection patterns

Best Practices for Production

  • Maintain at least 10-25 peers for reliable network participation
  • Alert when peer count drops below minimum thresholds (e.g., less than 5 peers)
  • Monitor peer count trends over time to identify gradual degradation
  • Combine with sync status checks to ensure connected peers are providing valid data
  • Review firewall rules and port configurations if peer count remains consistently low
  • Consider using static peers or bootnodes for guaranteed initial connections
  • Implement exponential backoff for peer count checks to reduce unnecessary load

Integration with Web3 Libraries

Using ethers.js

import { JsonRpcProvider } from 'ethers';

const provider = new JsonRpcProvider('https://xdc-rpc.dwellir.com');

async function monitorPeerConnections() {
try {
const hexPeerCount = await provider.send('net_peerCount', []);
const peerCount = parseInt(hexPeerCount, 16);

console.log(`Currently connected to ${peerCount} peers`);

if (peerCount < 5) {
console.warn('Low peer count detected. Node may be isolated.');
} else if (peerCount > 50) {
console.log('Excellent network connectivity');
}

return peerCount;
} catch (error) {
console.error('Failed to retrieve peer count:', error);
return 0;
}
}

// Monitor continuously
setInterval(async () => {
const peers = await monitorPeerConnections();
// Send to monitoring system
}, 30000); // Check every 30 seconds

Using web3.js

import Web3 from 'web3';

const web3 = new Web3('https://xdc-rpc.dwellir.com');

async function getPeerMetrics() {
const peerCount = await web3.eth.net.getPeerCount();
console.log(`Active peers: ${peerCount}`);

const [listening, networkId] = await Promise.all([
web3.eth.net.isListening(),
web3.eth.net.getId()
]);

return {
peers: peerCount,
listening,
networkId,
timestamp: new Date().toISOString()
};
}

Example Response

{
"jsonrpc": "2.0",
"id": 1,
"result": "0x19"
}

The hexadecimal value 0x19 equals 25 in decimal, indicating 25 connected peers.