P2P Chat Documentation

A concise guide to the P2P Chat project in Rust.

Features

  • Automatic peer discovery (UDP)
  • Real-time messaging (TCP)
  • Decentralized, no server
  • CLI interface
  • Heartbeat system
  • Threshold voting for secure-only messaging
  • Ed25519 cryptographic security

How It Works

  • Peers broadcast presence via UDP
  • Direct TCP connections for messages
  • CLI commands for messaging, proposals, voting, and status
  • Secure-only mode: unsigned messages rejected after threshold approval

🌐 Peer-to-Peer Networking Concepts

What is P2P?

Distributed architecture: peers act as both clients and servers. No central authority.

Key Concepts

  • Peer Discovery: UDP broadcast, decentralized
  • Direct Communication: TCP connections
  • Broadcast Messaging: Send to all peers
  • JSON Protocol: Structured messages
  • Network Resilience: Heartbeat system

P2P vs Client-Server

AspectP2P ChatClient-Server
ArchitectureDecentralizedCentralized
Failure PointsNo single pointServer failure breaks
ScalabilityScales with peersLimited by server
DiscoveryPeer-to-peerCentral directory
CommunicationDirectThrough server

Technical Details

  • UDP for discovery
  • TCP for messaging
  • Async Rust (tokio)
  • Ed25519 signatures

Real-World Examples

  • BitTorrent, Skype, Bitcoin, IPFS

Next Steps

  • Encryption, file sharing, private messaging, mobile support

🎓 P2P Networking Learning Summary

What We Built

  • Automatic peer discovery
  • Real-time messaging
  • Decentralized architecture
  • CLI interface
  • Robust networking

Key Concepts Learned

  • Service discovery
  • Direct communication
  • UDP vs TCP
  • Async programming
  • Protocol design
  • Distributed systems

Technical Skills

  • Rust async/await
  • UDP/TCP networking
  • Error handling
  • JSON serialization
  • CLI development

Real-World Applications

  • File sharing (BitTorrent, IPFS)
  • Communication (Skype, Discord)
  • Blockchain (Bitcoin, Ethereum)
  • Gaming networks

Next Steps

  • Message history, UI improvements, file sharing, private messages
  • Encryption, NAT traversal, GUI, mobile support
  • DHT, consensus, gossip protocols, blockchain

Key Takeaways

  • P2P: scalable, resilient, but complex
  • Rust: safe, performant, great for networking

🎬 Example Session Walkthrough

This walkthrough shows how P2P Chat works with multiple peers.

Terminal 1 - Alice

$ cargo run -- start --port 8080 --name Alice

🎙️  Starting P2P Chat...
👤 Your ID: abc123-def456-ghi789
📡 Your Name: Alice
🔌 Listening on port: 8080
🔗 TCP listener started on port 8080

💬

Terminal 2 - Bob (started 5 seconds later)

$ cargo run -- start --port 8081 --name Bob

🎙️  Starting P2P Chat...
👤 Your ID: xyz789-uvw456-rst123
📡 Your Name: Bob
🔌 Listening on port: 8081
🔗 TCP listener started on port 8081

💬

Peer Discovery (Alice's terminal)

🔍 Discovered new peer: Bob (192.168.1.101)
💬 /list
👥 Discovered peers:
  - Bob (xyz789-uvw456-rst123) at 192.168.1.101:8081

Messaging from Alice to Bob

💬 Hello Bob!
📤 Message sent to 1 peer(s)

Bob Receives the Message

📨 Alice says: Hello Bob!
💬 Hey Alice! Nice to meet you!
📤 Message sent to 1 peer(s)

Alice Receives Bob's Reply

📨 Bob says: Hey Alice! Nice to meet you!
💬

Adding Charlie (Terminal 3)

$ cargo run -- start --port 8082 --name Charlie

🎙️  Starting P2P Chat...
👤 Your ID: pqr456-stu789-vwx123
📡 Your Name: Charlie

# After discovery...
💬 /list
👥 Discovered peers:
  - Alice (abc123-def456-ghi789) at 192.168.1.100:8080
  - Bob (xyz789-uvw456-rst123) at 192.168.1.101:8081

💬 Hello everyone!
📤 Message sent to 2 peer(s)

All Terminals Receive Charlie's Message

Alice's terminal:

🔍 Discovered new peer: Charlie (192.168.1.102)
📨 Charlie says: Hello everyone!

Network Diagram

    Alice (8080)
        |  \\
        |   \\
        |    \\
    Bob (8081)---Charlie (8082)

UDP Discovery: All peers broadcast on port 9999
TCP Messages: Direct peer-to-peer connections

Key Observations

  • Peers auto-discover via UDP
  • Messages are sent directly over TCP
  • No central server
  • Secure messaging and threshold voting supported
  • CLI commands for messaging, proposals, voting, and status