> ## Documentation Index
> Fetch the complete documentation index at: https://docsdrop.erikraft.com/llms.txt
> Use this file to discover all available pages before exploring further.

# P2P Transfer

> Direct peer-to-peer file transfers using WebRTC DataChannels for secure, efficient communication.

# Peer-to-Peer File Transfer

ErikrafT Drop's core feature is direct peer-to-peer file transfer using WebRTC technology. This enables devices to communicate directly without routing files through central servers, providing both privacy and performance benefits.

## WebRTC DataChannel Implementation

### Connection Establishment

The P2P transfer system uses WebRTC DataChannels for direct communication:

```javascript theme={null}
// From network.js lines 1126-1129
const channel = this._conn.createDataChannel('data-channel', {
    ordered: true,
    reliable: true // Obsolete but maintained for compatibility
});
```

### Binary Data Transfer

Files are transferred as binary data for maximum efficiency:

```javascript theme={null}
// From network.js lines 1177-1178
channel.binaryType = 'arraybuffer';
channel.onmessage = e => this._onMessage(e.data);
```

## Transfer Capabilities

### Supported File Types

* **All File Types**: No restrictions on file formats
* **Large Files**: Limited only by browser memory and network constraints
* **Multiple Files**: Simultaneous transfer of multiple files and folders
* **Directory Support**: Complete directory structure preservation

### Transfer Performance

* **Direct Connection**: No server bandwidth consumption
* **Network Speed**: Limited only by local network capabilities
* **Concurrent Transfers**: Multiple simultaneous transfers supported
* **Resume Capability**: Automatic retry for interrupted transfers

## Security Features

### End-to-End Encryption

```javascript theme={null}
// WebRTC provides automatic DTLS/SRTP encryption
this._conn = new RTCPeerConnection(this.rtcConfig);
// All data is encrypted by the browser automatically
```

### Connection Authentication

* **Certificate Exchange**: WebRTC handles certificate exchange automatically
* **Fingerprint Verification**: Connection hashes for security validation
* **Perfect Forward Secrecy**: Each session uses unique encryption keys

## Transfer Process

### 1. Discovery Phase

Devices discover each other through:

* **Local Network**: Automatic IP-based discovery
* **Device Pairing**: Persistent connections using room secrets
* **Public Rooms**: Temporary 5-character room codes

### 2. Connection Phase

```javascript theme={null}
// From network.js lines 1104-1113
_connect() {
    if (!this._conn || this._conn.signalingState === "closed")
        this._openConnection();

    if (this._isCaller) {
        this._openChannel();
    } else {
        this._conn.ondatachannel = e => this._onChannelOpened(e);
    }
}
```

### 3. Transfer Phase

* **Request/Accept**: Transfer request and user confirmation
* **Metadata Exchange**: File information transmission
* **Chunked Transfer**: 64KB chunks for memory efficiency
* **Progress Tracking**: Real-time progress updates

## File Chunking System

### Chunk Size Optimization

```javascript theme={null}
// From network.js lines 1671-1672
this._chunkSize = 64000; // 64 KB chunks
this._maxPartitionSize = 1e6; // 1 MB partitions
```

### Memory Management

* **Streaming**: Files processed in chunks to prevent memory overload
* **Buffer Control**: Efficient buffer management for large files
* **iOS Optimization**: Special handling for iOS memory limits

## Network Optimization

### Connection Reuse

```javascript theme={null}
// From network.js lines 1287-1295
refresh() {
    if (this._isConnected() || this._isConnecting()) return;
    if (!this._isCaller) return;
    this._connect();
}
```

### Adaptive Performance

* **ICE Candidate Selection**: Optimal network path selection
* **Connection Monitoring**: Real-time connection quality assessment
* **Automatic Fallback**: WebSocket fallback when WebRTC fails

## Error Handling

### Connection Recovery

```javascript theme={null}
// From network.js lines 1241-1243
_onChannelClosed() {
    console.log('RTC: channel closed', this._peerId);
    Events.fire('peer-disconnected', this._peerId);
    if (!this._isCaller) return;
    this._connect(); // reopen the channel
}
```

### Transfer Reliability

* **Automatic Retry**: Failed chunks are automatically resent
* **Progress Preservation**: Transfer progress is maintained during retries
* **Graceful Degradation**: Fallback to WebSocket when necessary

## Performance Metrics

### Transfer Speed

* **Local Network**: Typically 50-200+ MB/s on Gigabit networks
* **WiFi Networks**: 10-50 MB/s depending on signal quality
* **Mobile Networks**: 1-10 MB/s depending on connection quality

### Latency

* **Connection Setup**: 1-3 seconds for WebRTC establishment
* **Transfer Latency**: Sub-millisecond for local networks
* **Response Time**: Real-time progress updates

## Browser Compatibility

### WebRTC Support Detection

```javascript theme={null}
// From network.js lines 1425-1434
if (window.isRtcSupported && rtcSupported) {
    this.peers[peerId] = new RTCPeer(this._server, isCaller, peerId, roomType, roomId, this._wsConfig.rtcConfig);
} else if (this._wsConfig.wsFallback) {
    this.peers[peerId] = new WSPeer(this._server, isCaller, peerId, roomType, roomId);
}
```

### Supported Browsers

* **Chrome 23+**: Full WebRTC support
* **Firefox 22+**: Full WebRTC support
* **Safari 11+**: WebRTC support with some limitations
* **Edge 79+**: Full WebRTC support (Chromium-based)

## Advanced Features

### Connection Hashing

```javascript theme={null}
// From network.js lines 1193-1217
getConnectionHash() {
    const localDescriptionLines = this._conn.localDescription.sdp.split("\r\n");
    const remoteDescriptionLines = this._conn.remoteDescription.sdp.split("\r\n");
    // Extract and combine fingerprints for unique connection identification
    const combinedFingerprints = this._isCaller
        ? localConnectionFingerprint + remoteConnectionFingerprint
        : remoteConnectionFingerprint + localConnectionFingerprint;
    return cyrb53(combinedFingerprints).toString();
}
```

### Multi-Room Support

* **Simultaneous Rooms**: Single peer in multiple room types
* **Flexible Discovery**: IP-based, secret-based, and public rooms
* **Dynamic Switching**: Seamless switching between room types

## Use Cases

### Personal File Sharing

* **Document Transfer**: Quick sharing between work and personal devices
* **Media Sharing**: Photo and video sharing between devices
* **Backup**: Simple backup between computers and mobile devices

### Professional Use

* **Collaboration**: File sharing during meetings
* **Development**: Quick code and asset sharing
* **Presentations**: Transfer presentation files between devices

### Emergency Scenarios

* **No Internet**: Works completely offline on local networks
* **Remote Areas**: No dependency on cloud services
* **Privacy**: No third-party access to transferred files

This P2P transfer system provides a robust, secure, and efficient method for direct file sharing between devices while maintaining user privacy and maximizing transfer speeds.
