Core Concepts
Understanding these fundamental concepts will help you build better P2P applications with LiteP2P.
Peers
A peer is any device or application instance connected to the LiteP2P network. Each peer has:
- Peer ID – A unique identifier generated when the SDK is initialized
- Public Key – Used for end-to-end encryption and identity verification
- Capabilities – What the peer can do (messaging, file transfer, relay, etc.)
// Get your peer's ID
val myPeerId = LiteP2P.getInstance().peerId
Log.d("LiteP2P", "My Peer ID: $myPeerId")
Connections
Connections are the links between peers. LiteP2P supports multiple connection types:
| Type | Description | Use Case |
|---|---|---|
| Direct | Peer-to-peer without intermediary | Best latency, requires NAT traversal |
| Relayed | Through a relay server | Fallback when direct fails |
| Hybrid | Starts relayed, upgrades to direct | Default mode, best of both |
NAT Traversal
Most devices are behind Network Address Translation (NAT), which makes direct connections challenging. LiteP2P handles this automatically using:
- STUN – Discovers your public IP and port
- TURN – Relay fallback when direct connection fails
- ICE – Finds the best connection path
- Hole Punching – Creates direct paths through NATs
You don't need to implement NAT traversal yourself. LiteP2P handles all of this automatically. Just call connect() and we'll find the best path.
End-to-End Encryption
All LiteP2P communications are encrypted by default:
- Key Exchange – X25519 Elliptic Curve Diffie-Hellman
- Symmetric Encryption – ChaCha20-Poly1305
- Identity Verification – Ed25519 signatures
- Perfect Forward Secrecy – New keys for each session
// Encryption is enabled by default
val config = PeerConfig.Builder()
.setAppId("your-app-id")
.enableEncryption(true) // Default: true
.build()
Messaging
LiteP2P provides reliable message delivery with these guarantees:
- Ordered Delivery – Messages arrive in the order sent
- Reliability – Automatic retransmission on failure
- Acknowledgments – Know when messages are delivered
- Offline Queuing – Messages stored until peer reconnects
// Send a message
p2p.sendMessage(peerId, data) { result ->
when (result) {
is SendResult.Delivered -> Log.d("LiteP2P", "Message delivered!")
is SendResult.Queued -> Log.d("LiteP2P", "Peer offline, queued")
is SendResult.Error -> Log.e("LiteP2P", "Failed: ${result.message}")
}
}
Peer Discovery
Finding other peers in the network:
- Known Peer ID – Connect directly if you know the peer's ID
- Topic-Based – Subscribe to topics and discover peers
- Nearby Discovery – Find peers on the same local network
- Bootstrapping – Connect to well-known bootstrap nodes
// Subscribe to a topic
p2p.subscribe("my-app-room-123") { event ->
when (event) {
is PeerJoined -> Log.d("LiteP2P", "Peer joined: ${event.peerId}")
is PeerLeft -> Log.d("LiteP2P", "Peer left: ${event.peerId}")
}
}
Network Layer
LiteP2P operates on top of existing transport protocols:
- WebRTC – For browser and cross-platform compatibility
- QUIC – For high-performance native applications
- TCP – Fallback for restricted networks
The SDK automatically selects the best transport based on platform capabilities and network conditions.
Architecture Overview
┌─────────────────────────────────────────┐
│ Your Application │
├─────────────────────────────────────────┤
│ LiteP2P SDK │
│ ┌─────────┬──────────┬──────────────┐ │
│ │Messaging│ Files │ Discovery │ │
│ ├─────────┴──────────┴──────────────┤ │
│ │ Connection Manager │ │
│ ├───────────────────────────────────┤ │
│ │ Encryption │ NAT Traversal │ │
│ ├───────────────────────────────────┤ │
│ │ QUIC │ WebRTC │ TCP │ │
│ └───────────────────────────────────┘ │
├─────────────────────────────────────────┤
│ Network │
└─────────────────────────────────────────┘