A proof-of-concept application demonstrating core libp2p concepts through a decentralized chat interface. This project showcases peer discovery mechanisms, pubsub patterns, and real-time network monitoring in a practical P2P environment.
This application serves as a practical demonstration of key libp2p features:
- Hybrid peer discovery (mDNS + Kademlia DHT)
- PubSub messaging patterns
- Real-time network event monitoring
- P2P connection management
- Protocol negotiation
- Uses multicast DNS for zero-configuration networking
- Automatic peer discovery within the same LAN
- Implementation of the mDNS discovery service interface
- Configurable retry mechanisms and connection handling
type MDNSDiscovery struct {
host host.Host
peerChan chan peer.AddrInfo
// ... other fields
}
- Distributed Hash Table based on Kademlia protocol
- Provides decentralized peer discovery across the internet
- Uses bootstrap nodes for initial network joining
- Implements key-based routing with XOR metric
type DHTDiscovery struct {
dht *dht.IpfsDHT
discovery *routing.RoutingDiscovery
// ... other fields
}
- Built on libp2p's GossipSub protocol
- Topic-based message routing
- Automatic peer discovery and mesh formation
- Configurable message validation and scoring
- Event-driven architecture tracking network state
- Monitored events include:
- Peer connections/disconnections
- Protocol negotiations
- NAT traversal status
- Local/remote address updates
- Peer identification lifecycle
The application provides real-time monitoring of:
// Metrics updated every 60 seconds
- Bandwidth usage (in/out rates)
- PubSub-specific bandwidth
- Connected peers count
- Active protocols
- Memory utilization
git clone https://github.com/alejoacosta74/p2p-chat
cd p2p-chat
go build
Usage: p2p-chat [flags]
Flags:
-n, --nickname string Nickname for chat (default "anonymous")
-r, --room string Chat room name (default "default")
-l, --log string Log level [debug|info|warn|error] (default "info")
-f, --logfile string Log file name (default "chat.log")
Start with debug logging to see detailed libp2p events:
./p2p-chat --log debug
This enables the real-time event panel showing:
- Protocol negotiations
- Peer discovery events
- Connection state changes
- NAT traversal updates
- DHT operations
- Message Area: Chat messages with colored nicknames
- Peer List: Active participants in current room
- Event Log Panel: Real-time libp2p events and metrics
- Input Field: Message composition with commands
- Real-time libp2p event streaming
- Network metrics updates
- Protocol negotiation logs
- Peer discovery events
- Connection state changes
- Custom peer scoring implementation
- Advanced DHT configuration options
- Alternative transport protocols (QUIC, WebRTC)
- Custom protocol handlers
- Custom event subscribers
- Performance profiling tools
- Noise protocol integration
- Custom PubSub message validation
- Peer authentication mechanisms
- Rate limiting implementation
This project serves as a learning tool for understanding:
- P2P network architecture
- Distributed systems concepts
- libp2p protocol stack
- Network event handling
- Decentralized discovery mechanisms
MIT License - see LICENSE
Built with libp2p | UI powered by tview