peerpigeon 1.0.4 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -1
- package/dist/peerpigeon-browser.js +800 -36
- package/dist/peerpigeon-browser.js.map +3 -3
- package/docs/BINARY_IMPLEMENTATION_SUMMARY.md +304 -0
- package/docs/BINARY_MESSAGES.md +332 -0
- package/docs/BROADCAST_STREAMING.md +388 -0
- package/docs/PEER_DISCOVERY_FIXES.md +140 -0
- package/docs/STREAMING_API.md +542 -0
- package/docs/STREAMING_IMPLEMENTATION_SUMMARY.md +286 -0
- package/examples/binary-message-demo.html +496 -0
- package/examples/broadcast-stream-demo.html +539 -0
- package/examples/browser-2/EXPRESS_SOLUTION.md +183 -0
- package/examples/browser-2/ICE_DEBUG.md +171 -0
- package/examples/browser-2/IMPLEMENTATION.md +218 -0
- package/examples/browser-2/QUICK_START.md +149 -0
- package/examples/browser-2/README.md +303 -0
- package/examples/browser-2/dist/assets/index-Cck_6WJV.css +1 -0
- package/examples/browser-2/dist/assets/index-D8-cNlWQ.js +57 -0
- package/examples/browser-2/dist/index.html +32 -0
- package/examples/browser-2/dist/peerpigeon-browser.js +12483 -0
- package/examples/browser-2/index.html +2 -1
- package/examples/browser-2/package-lock.json +1276 -0
- package/examples/browser-2/package.json +5 -3
- package/examples/browser-2/public/peerpigeon-browser.js +12483 -0
- package/examples/browser-2/src/App.vue +7 -4
- package/examples/browser-2/src/components/CryptoView.vue +4 -4
- package/examples/browser-2/src/components/TestingView.vue +794 -0
- package/examples/browser-2/src/main.js +7 -1
- package/examples/browser-2/src/router.js +63 -0
- package/examples/browser-2/src/stores/peerpigeon.js +68 -7
- package/examples/browser-2/vite.config.js +15 -1
- package/examples/stream-file-transfer-demo.html +935 -0
- package/package.json +1 -1
- package/server/index.js +175 -38
- package/src/ConnectionManager.js +68 -8
- package/src/GossipManager.js +22 -0
- package/src/PeerConnection.js +421 -11
- package/src/PeerDiscovery.js +33 -18
- package/src/PeerPigeonMesh.js +453 -1
- package/src/SignalingClient.js +12 -5
- package/src/SignalingHandler.js +18 -5
- package/src/browser-entry.js +5 -1
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# Binary Message Support Implementation Summary
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Successfully implemented native binary message support in PeerPigeon, eliminating the need for JSON serialization of binary data (Uint8Array, ArrayBuffer, etc.).
|
|
6
|
+
|
|
7
|
+
## Problem Solved
|
|
8
|
+
|
|
9
|
+
**Before**: PeerPigeon used `JSON.stringify()` to serialize all messages, which:
|
|
10
|
+
- Corrupted binary data (converted to objects like `{"0": 1, "1": 2, ...}`)
|
|
11
|
+
- Added significant overhead (33%+ size increase with base64)
|
|
12
|
+
- Reduced performance
|
|
13
|
+
- Required manual encoding/decoding
|
|
14
|
+
|
|
15
|
+
**After**: Binary data is sent directly over WebRTC data channels:
|
|
16
|
+
- Preserves binary data integrity
|
|
17
|
+
- No serialization overhead
|
|
18
|
+
- Direct ArrayBuffer transfer
|
|
19
|
+
- Efficient and fast
|
|
20
|
+
|
|
21
|
+
## Changes Made
|
|
22
|
+
|
|
23
|
+
### 1. Core WebRTC Layer (`PeerConnection.js`)
|
|
24
|
+
|
|
25
|
+
#### Constructor
|
|
26
|
+
Added binary message state tracking:
|
|
27
|
+
```javascript
|
|
28
|
+
this._expectingBinaryMessage = false;
|
|
29
|
+
this._binaryMessageSize = 0;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
#### `sendMessage()` Method
|
|
33
|
+
Enhanced to detect and handle binary data:
|
|
34
|
+
```javascript
|
|
35
|
+
sendMessage(message) {
|
|
36
|
+
if (message instanceof ArrayBuffer || ArrayBuffer.isView(message)) {
|
|
37
|
+
// Send binary header
|
|
38
|
+
dataChannel.send(JSON.stringify({ type: '__BINARY__', size }));
|
|
39
|
+
// Send binary payload
|
|
40
|
+
dataChannel.send(buffer);
|
|
41
|
+
} else {
|
|
42
|
+
// Regular JSON message
|
|
43
|
+
dataChannel.send(JSON.stringify(message));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### `onmessage` Handler
|
|
49
|
+
Enhanced to receive and parse binary messages:
|
|
50
|
+
```javascript
|
|
51
|
+
dataChannel.onmessage = (event) => {
|
|
52
|
+
if (event.data instanceof ArrayBuffer) {
|
|
53
|
+
// Handle binary payload
|
|
54
|
+
const uint8Array = new Uint8Array(event.data);
|
|
55
|
+
emit('message', { type: 'binary', data: uint8Array, size });
|
|
56
|
+
} else {
|
|
57
|
+
// Handle JSON or binary header
|
|
58
|
+
const message = JSON.parse(event.data);
|
|
59
|
+
if (message.type === '__BINARY__') {
|
|
60
|
+
this._expectingBinaryMessage = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 2. Connection Manager (`ConnectionManager.js`)
|
|
67
|
+
|
|
68
|
+
#### `handleIncomingMessage()` Method
|
|
69
|
+
Added binary message routing:
|
|
70
|
+
```javascript
|
|
71
|
+
handleIncomingMessage(message, fromPeerId) {
|
|
72
|
+
if (message.type === 'binary' && message.data instanceof Uint8Array) {
|
|
73
|
+
this.mesh.emit('binaryMessageReceived', {
|
|
74
|
+
from: fromPeerId,
|
|
75
|
+
data: message.data,
|
|
76
|
+
size: message.size,
|
|
77
|
+
timestamp: Date.now()
|
|
78
|
+
});
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
// ... rest of message handling
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 3. Mesh API (`PeerPigeonMesh.js`)
|
|
86
|
+
|
|
87
|
+
#### New Methods
|
|
88
|
+
|
|
89
|
+
**Send to Specific Peer:**
|
|
90
|
+
```javascript
|
|
91
|
+
async sendBinaryData(targetPeerId, binaryData) {
|
|
92
|
+
const peerConnection = this.connectionManager.peers.get(targetPeerId);
|
|
93
|
+
return peerConnection.sendMessage(binaryData);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Broadcast to All:**
|
|
98
|
+
```javascript
|
|
99
|
+
async broadcastBinaryData(binaryData) {
|
|
100
|
+
const peers = this.getConnectedPeers();
|
|
101
|
+
let sentCount = 0;
|
|
102
|
+
for (const peer of peers) {
|
|
103
|
+
if (await this.sendBinaryData(peer.peerId, binaryData)) {
|
|
104
|
+
sentCount++;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return sentCount;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 4. TypeScript Definitions (`types/index.d.ts`)
|
|
112
|
+
|
|
113
|
+
Added new interfaces and methods:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
// Event interface
|
|
117
|
+
export interface BinaryMessageEvent {
|
|
118
|
+
from: string;
|
|
119
|
+
data: Uint8Array;
|
|
120
|
+
size: number;
|
|
121
|
+
timestamp: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Methods
|
|
125
|
+
sendBinaryData(targetPeerId: string, binaryData: Uint8Array | ArrayBuffer): Promise<boolean>;
|
|
126
|
+
broadcastBinaryData(binaryData: Uint8Array | ArrayBuffer): Promise<number>;
|
|
127
|
+
|
|
128
|
+
// Events
|
|
129
|
+
addEventListener(event: 'binaryMessageReceived', listener: (data: BinaryMessageEvent) => void): void;
|
|
130
|
+
on(event: 'binaryMessageReceived', listener: (data: BinaryMessageEvent) => void): this;
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## New API
|
|
134
|
+
|
|
135
|
+
### Sending Binary Data
|
|
136
|
+
|
|
137
|
+
```javascript
|
|
138
|
+
// To specific peer
|
|
139
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
140
|
+
await mesh.sendBinaryData(targetPeerId, data);
|
|
141
|
+
|
|
142
|
+
// Broadcast to all
|
|
143
|
+
const count = await mesh.broadcastBinaryData(data);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Receiving Binary Data
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
mesh.addEventListener('binaryMessageReceived', (event) => {
|
|
150
|
+
const { from, data, size } = event;
|
|
151
|
+
console.log(`Received ${size} bytes from ${from}`);
|
|
152
|
+
// data is Uint8Array
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Documentation Created
|
|
157
|
+
|
|
158
|
+
1. **`docs/BINARY_MESSAGES.md`** - Complete guide with:
|
|
159
|
+
- API documentation
|
|
160
|
+
- Usage examples (text, files, images, chunking)
|
|
161
|
+
- Performance considerations
|
|
162
|
+
- Best practices
|
|
163
|
+
- Troubleshooting
|
|
164
|
+
|
|
165
|
+
2. **`examples/binary-message-demo.html`** - Interactive demo showing:
|
|
166
|
+
- Random binary data transmission
|
|
167
|
+
- Test patterns
|
|
168
|
+
- Text encoding/decoding
|
|
169
|
+
- Peer selection (unicast/broadcast)
|
|
170
|
+
- Real-time stats and hex dumps
|
|
171
|
+
- Activity logging
|
|
172
|
+
|
|
173
|
+
## Technical Details
|
|
174
|
+
|
|
175
|
+
### Message Flow
|
|
176
|
+
|
|
177
|
+
1. **Sender**: Detects binary → Sends header → Sends payload
|
|
178
|
+
2. **Receiver**: Receives header → Sets flag → Receives payload → Emits event
|
|
179
|
+
3. **Application**: Listens to `binaryMessageReceived` event
|
|
180
|
+
|
|
181
|
+
### Protocol
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
[Header Message - JSON]
|
|
185
|
+
{ "type": "__BINARY__", "size": 1024 }
|
|
186
|
+
|
|
187
|
+
[Binary Payload - ArrayBuffer]
|
|
188
|
+
<raw binary data>
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Backward Compatibility
|
|
192
|
+
|
|
193
|
+
- JSON messages work exactly as before
|
|
194
|
+
- No breaking changes to existing code
|
|
195
|
+
- Binary support is additive
|
|
196
|
+
|
|
197
|
+
## Performance Benefits
|
|
198
|
+
|
|
199
|
+
| Metric | JSON + Base64 | Native Binary |
|
|
200
|
+
|--------|---------------|---------------|
|
|
201
|
+
| Size Overhead | +33% | 0% |
|
|
202
|
+
| Serialization | Yes | No |
|
|
203
|
+
| Type Conversion | Yes | No |
|
|
204
|
+
| Data Integrity | Lossy | Perfect |
|
|
205
|
+
| Speed | Slower | Faster |
|
|
206
|
+
|
|
207
|
+
## Use Cases
|
|
208
|
+
|
|
209
|
+
- **File transfers**: Send files peer-to-peer
|
|
210
|
+
- **Media streaming**: Raw audio/video data
|
|
211
|
+
- **Binary protocols**: Custom protocols
|
|
212
|
+
- **Encryption**: Send encrypted data
|
|
213
|
+
- **Large datasets**: Scientific/analytics data
|
|
214
|
+
- **Images**: Canvas pixel data
|
|
215
|
+
|
|
216
|
+
## Testing
|
|
217
|
+
|
|
218
|
+
The demo can be tested by:
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
# Terminal 1: Start signaling server
|
|
222
|
+
npm run hub
|
|
223
|
+
|
|
224
|
+
# Terminal 2: Start HTTP server
|
|
225
|
+
npm run http
|
|
226
|
+
|
|
227
|
+
# Open in multiple browser tabs
|
|
228
|
+
http://localhost:8080/examples/binary-message-demo.html
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Browser Support
|
|
232
|
+
|
|
233
|
+
Works in all modern browsers with:
|
|
234
|
+
- WebRTC RTCDataChannel ✅
|
|
235
|
+
- ArrayBuffer/Uint8Array ✅
|
|
236
|
+
- TextEncoder/Decoder ✅
|
|
237
|
+
|
|
238
|
+
Tested on: Chrome, Firefox, Safari, Edge
|
|
239
|
+
|
|
240
|
+
## Future Enhancements
|
|
241
|
+
|
|
242
|
+
Possible improvements:
|
|
243
|
+
- [ ] Automatic chunking for large files
|
|
244
|
+
- [ ] Built-in compression
|
|
245
|
+
- [ ] Progress callbacks
|
|
246
|
+
- [ ] Streaming API
|
|
247
|
+
- [ ] Binary encryption
|
|
248
|
+
- [ ] Reliability layer with ACKs
|
|
249
|
+
|
|
250
|
+
## Files Modified
|
|
251
|
+
|
|
252
|
+
1. `src/PeerConnection.js` - Core binary handling
|
|
253
|
+
2. `src/ConnectionManager.js` - Message routing
|
|
254
|
+
3. `src/PeerPigeonMesh.js` - Public API methods
|
|
255
|
+
4. `types/index.d.ts` - TypeScript definitions
|
|
256
|
+
|
|
257
|
+
## Files Created
|
|
258
|
+
|
|
259
|
+
1. `docs/BINARY_MESSAGES.md` - Documentation
|
|
260
|
+
2. `examples/binary-message-demo.html` - Interactive demo
|
|
261
|
+
3. `docs/BINARY_IMPLEMENTATION_SUMMARY.md` - This file
|
|
262
|
+
|
|
263
|
+
## Verification
|
|
264
|
+
|
|
265
|
+
Build completed successfully:
|
|
266
|
+
```
|
|
267
|
+
✅ Browser bundle created at dist/peerpigeon-browser.js
|
|
268
|
+
📦 Binary message support included
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Example Usage
|
|
272
|
+
|
|
273
|
+
```javascript
|
|
274
|
+
// Initialize mesh
|
|
275
|
+
const mesh = new PeerPigeonMesh({
|
|
276
|
+
signalingServer: 'ws://localhost:3000',
|
|
277
|
+
autoConnect: true
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
await mesh.connect();
|
|
281
|
+
|
|
282
|
+
// Send binary data
|
|
283
|
+
const data = new Uint8Array(1024);
|
|
284
|
+
crypto.getRandomValues(data);
|
|
285
|
+
await mesh.broadcastBinaryData(data);
|
|
286
|
+
|
|
287
|
+
// Receive binary data
|
|
288
|
+
mesh.addEventListener('binaryMessageReceived', (event) => {
|
|
289
|
+
console.log(`Received ${event.size} bytes from ${event.from}`);
|
|
290
|
+
processData(event.data); // Uint8Array
|
|
291
|
+
});
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## Conclusion
|
|
295
|
+
|
|
296
|
+
Binary message support is now fully implemented and ready for use. The feature:
|
|
297
|
+
- ✅ Works transparently with existing code
|
|
298
|
+
- ✅ Maintains backward compatibility
|
|
299
|
+
- ✅ Provides significant performance benefits
|
|
300
|
+
- ✅ Includes comprehensive documentation
|
|
301
|
+
- ✅ Has interactive demo for testing
|
|
302
|
+
- ✅ Supports all major browsers
|
|
303
|
+
|
|
304
|
+
Users can now send binary data efficiently without JSON serialization overhead!
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# Binary Message Support
|
|
2
|
+
|
|
3
|
+
PeerPigeon now supports sending and receiving binary data (Uint8Array, ArrayBuffer, etc.) directly over WebRTC data channels without JSON serialization overhead.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Prior to this feature, all messages were JSON-serialized, which made sending binary data inefficient and potentially corrupted the data. The new binary message support allows you to send raw binary data efficiently for use cases like:
|
|
8
|
+
|
|
9
|
+
- File transfers
|
|
10
|
+
- Image/video streaming
|
|
11
|
+
- Audio data
|
|
12
|
+
- Binary protocols
|
|
13
|
+
- Encrypted data
|
|
14
|
+
- Large datasets
|
|
15
|
+
- Custom binary formats
|
|
16
|
+
|
|
17
|
+
## How It Works
|
|
18
|
+
|
|
19
|
+
When you send binary data:
|
|
20
|
+
|
|
21
|
+
1. **Detection**: PeerPigeon detects if the message is an ArrayBuffer or TypedArray (like Uint8Array)
|
|
22
|
+
2. **Header**: A small JSON header is sent first indicating binary data follows
|
|
23
|
+
3. **Binary Transfer**: The actual binary data is sent directly over the data channel
|
|
24
|
+
4. **Reception**: The receiver gets a `binaryMessageReceived` event with the Uint8Array data
|
|
25
|
+
|
|
26
|
+
This approach preserves the binary data perfectly while maintaining backward compatibility with JSON messages.
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
### Sending Binary Data
|
|
31
|
+
|
|
32
|
+
#### Send to Specific Peer
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
const mesh = new PeerPigeonMesh({ /* options */ });
|
|
36
|
+
|
|
37
|
+
// Create binary data
|
|
38
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
39
|
+
|
|
40
|
+
// Send to specific peer
|
|
41
|
+
const success = await mesh.sendBinaryData(targetPeerId, data);
|
|
42
|
+
console.log(`Sent: ${success}`);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### Broadcast to All Peers
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
// Broadcast binary data to all connected peers
|
|
49
|
+
const sentCount = await mesh.broadcastBinaryData(data);
|
|
50
|
+
console.log(`Sent to ${sentCount} peers`);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Receiving Binary Data
|
|
54
|
+
|
|
55
|
+
Listen for the `binaryMessageReceived` event:
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
mesh.addEventListener('binaryMessageReceived', (event) => {
|
|
59
|
+
const { from, data, size } = event;
|
|
60
|
+
|
|
61
|
+
console.log(`Received ${size} bytes from ${from}`);
|
|
62
|
+
console.log(`Data:`, data); // Uint8Array
|
|
63
|
+
|
|
64
|
+
// Use the binary data
|
|
65
|
+
processData(data);
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Examples
|
|
70
|
+
|
|
71
|
+
### Example 1: Send Text as Binary
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
// Encode text as binary
|
|
75
|
+
const encoder = new TextEncoder();
|
|
76
|
+
const text = "Hello, PeerPigeon!";
|
|
77
|
+
const binaryData = encoder.encode(text);
|
|
78
|
+
|
|
79
|
+
// Send
|
|
80
|
+
await mesh.sendBinaryData(targetPeerId, binaryData);
|
|
81
|
+
|
|
82
|
+
// Receive and decode
|
|
83
|
+
mesh.addEventListener('binaryMessageReceived', (event) => {
|
|
84
|
+
const decoder = new TextDecoder();
|
|
85
|
+
const text = decoder.decode(event.data);
|
|
86
|
+
console.log('Received text:', text);
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Example 2: Send Random Binary Data
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
// Generate random binary data
|
|
94
|
+
const size = 1024; // 1KB
|
|
95
|
+
const randomData = new Uint8Array(size);
|
|
96
|
+
crypto.getRandomValues(randomData);
|
|
97
|
+
|
|
98
|
+
// Broadcast to all peers
|
|
99
|
+
const count = await mesh.broadcastBinaryData(randomData);
|
|
100
|
+
console.log(`Broadcasted ${size} bytes to ${count} peers`);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Example 3: Send File Data
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
// Read file as binary
|
|
107
|
+
const fileInput = document.getElementById('file-input');
|
|
108
|
+
const file = fileInput.files[0];
|
|
109
|
+
|
|
110
|
+
const arrayBuffer = await file.arrayBuffer();
|
|
111
|
+
const uint8Array = new Uint8Array(arrayBuffer);
|
|
112
|
+
|
|
113
|
+
// Send file data
|
|
114
|
+
await mesh.sendBinaryData(targetPeerId, uint8Array);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Example 4: Send Image Data
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
// Get image data from canvas
|
|
121
|
+
const canvas = document.getElementById('myCanvas');
|
|
122
|
+
const ctx = canvas.getContext('2d');
|
|
123
|
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
124
|
+
|
|
125
|
+
// Send image data as binary
|
|
126
|
+
await mesh.sendBinaryData(targetPeerId, imageData.data);
|
|
127
|
+
|
|
128
|
+
// Receive and display
|
|
129
|
+
mesh.addEventListener('binaryMessageReceived', (event) => {
|
|
130
|
+
const receivedCanvas = document.getElementById('receivedCanvas');
|
|
131
|
+
const ctx = receivedCanvas.getContext('2d');
|
|
132
|
+
const imageData = ctx.createImageData(width, height);
|
|
133
|
+
imageData.data.set(event.data);
|
|
134
|
+
ctx.putImageData(imageData, 0, 0);
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Example 5: Chunked File Transfer
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
// Send large file in chunks
|
|
142
|
+
async function sendFileInChunks(peerId, file, chunkSize = 16384) {
|
|
143
|
+
const arrayBuffer = await file.arrayBuffer();
|
|
144
|
+
const totalChunks = Math.ceil(arrayBuffer.byteLength / chunkSize);
|
|
145
|
+
|
|
146
|
+
for (let i = 0; i < totalChunks; i++) {
|
|
147
|
+
const start = i * chunkSize;
|
|
148
|
+
const end = Math.min(start + chunkSize, arrayBuffer.byteLength);
|
|
149
|
+
const chunk = new Uint8Array(arrayBuffer.slice(start, end));
|
|
150
|
+
|
|
151
|
+
await mesh.sendBinaryData(peerId, chunk);
|
|
152
|
+
console.log(`Sent chunk ${i + 1}/${totalChunks}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Event Details
|
|
158
|
+
|
|
159
|
+
### binaryMessageReceived Event
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
interface BinaryMessageEvent {
|
|
163
|
+
from: string; // Peer ID of sender
|
|
164
|
+
data: Uint8Array; // Binary data
|
|
165
|
+
size: number; // Size in bytes
|
|
166
|
+
timestamp: number; // When received (Date.now())
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Performance Considerations
|
|
171
|
+
|
|
172
|
+
### Advantages of Binary Messages
|
|
173
|
+
|
|
174
|
+
- **Efficiency**: No JSON serialization overhead
|
|
175
|
+
- **Integrity**: Binary data is preserved exactly
|
|
176
|
+
- **Speed**: Direct ArrayBuffer transfer is faster
|
|
177
|
+
- **Size**: No base64 encoding bloat (33% overhead)
|
|
178
|
+
|
|
179
|
+
### Size Limits
|
|
180
|
+
|
|
181
|
+
WebRTC data channels have a maximum message size limit (typically 16KB-64KB depending on browser). For larger data:
|
|
182
|
+
|
|
183
|
+
1. **Chunking**: Split data into smaller chunks
|
|
184
|
+
2. **Streaming**: Send data progressively
|
|
185
|
+
3. **Compression**: Use compression libraries before sending
|
|
186
|
+
|
|
187
|
+
```javascript
|
|
188
|
+
// Example: Chunk large data
|
|
189
|
+
function chunkData(data, chunkSize = 16384) {
|
|
190
|
+
const chunks = [];
|
|
191
|
+
for (let i = 0; i < data.length; i += chunkSize) {
|
|
192
|
+
chunks.push(data.slice(i, i + chunkSize));
|
|
193
|
+
}
|
|
194
|
+
return chunks;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Send chunks
|
|
198
|
+
for (const chunk of chunkData(largeData)) {
|
|
199
|
+
await mesh.sendBinaryData(peerId, chunk);
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Comparison with JSON Messages
|
|
204
|
+
|
|
205
|
+
### Before (JSON Serialization)
|
|
206
|
+
|
|
207
|
+
```javascript
|
|
208
|
+
// Binary data must be converted to base64
|
|
209
|
+
const base64 = btoa(String.fromCharCode(...binaryData));
|
|
210
|
+
await mesh.sendMessage({ type: 'binary', data: base64 });
|
|
211
|
+
|
|
212
|
+
// 33% size increase due to base64 encoding
|
|
213
|
+
// JSON serialization overhead
|
|
214
|
+
// Data type conversion overhead
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### After (Native Binary)
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
// Send binary data directly
|
|
221
|
+
await mesh.sendBinaryData(peerId, binaryData);
|
|
222
|
+
|
|
223
|
+
// No size increase
|
|
224
|
+
// No serialization overhead
|
|
225
|
+
// Direct ArrayBuffer transfer
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Demo
|
|
229
|
+
|
|
230
|
+
Run the included demo to see binary messages in action:
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
npm run dev
|
|
234
|
+
# Open http://localhost:8080/examples/binary-message-demo.html
|
|
235
|
+
# Open in multiple browser tabs to test
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
The demo allows you to:
|
|
239
|
+
- Send random binary data
|
|
240
|
+
- Send test patterns
|
|
241
|
+
- Send text as binary
|
|
242
|
+
- Broadcast to all peers
|
|
243
|
+
- Monitor bytes sent/received
|
|
244
|
+
- View hex dumps of data
|
|
245
|
+
|
|
246
|
+
## Browser Compatibility
|
|
247
|
+
|
|
248
|
+
Binary message support works in all browsers that support:
|
|
249
|
+
- WebRTC RTCDataChannel
|
|
250
|
+
- ArrayBuffer
|
|
251
|
+
- Uint8Array
|
|
252
|
+
- TextEncoder/TextDecoder (for text encoding)
|
|
253
|
+
|
|
254
|
+
This includes all modern browsers (Chrome, Firefox, Safari, Edge).
|
|
255
|
+
|
|
256
|
+
## Implementation Details
|
|
257
|
+
|
|
258
|
+
### Message Flow
|
|
259
|
+
|
|
260
|
+
1. **Sender Side** (`PeerConnection.sendMessage`):
|
|
261
|
+
```javascript
|
|
262
|
+
if (message instanceof ArrayBuffer || ArrayBuffer.isView(message)) {
|
|
263
|
+
// Send header
|
|
264
|
+
dataChannel.send(JSON.stringify({ type: '__BINARY__', size }));
|
|
265
|
+
// Send binary data
|
|
266
|
+
dataChannel.send(buffer);
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
2. **Receiver Side** (`PeerConnection.onmessage`):
|
|
271
|
+
```javascript
|
|
272
|
+
if (event.data instanceof ArrayBuffer) {
|
|
273
|
+
if (this._expectingBinaryMessage) {
|
|
274
|
+
emit('message', { type: 'binary', data: new Uint8Array(event.data) });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
3. **Application Level** (`ConnectionManager.handleIncomingMessage`):
|
|
280
|
+
```javascript
|
|
281
|
+
if (message.type === 'binary') {
|
|
282
|
+
mesh.emit('binaryMessageReceived', { from, data, size });
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Troubleshooting
|
|
287
|
+
|
|
288
|
+
### Binary Data Not Received
|
|
289
|
+
|
|
290
|
+
- Check browser console for errors
|
|
291
|
+
- Ensure both peers have updated PeerPigeon version
|
|
292
|
+
- Verify data channel is open (`peer.dataChannel.readyState === 'open'`)
|
|
293
|
+
- Check data size isn't exceeding limits
|
|
294
|
+
|
|
295
|
+
### Data Corruption
|
|
296
|
+
|
|
297
|
+
- Don't modify the Uint8Array while sending
|
|
298
|
+
- Don't try to JSON.stringify binary data
|
|
299
|
+
- Use correct TypedArray types for your data
|
|
300
|
+
|
|
301
|
+
### Performance Issues
|
|
302
|
+
|
|
303
|
+
- Chunk large files into smaller pieces
|
|
304
|
+
- Use compression for repetitive data
|
|
305
|
+
- Monitor network bandwidth
|
|
306
|
+
- Consider adding flow control for large transfers
|
|
307
|
+
|
|
308
|
+
## Best Practices
|
|
309
|
+
|
|
310
|
+
1. **Size Management**: Keep messages under 16KB or implement chunking
|
|
311
|
+
2. **Error Handling**: Always check return values and handle errors
|
|
312
|
+
3. **Type Safety**: Use proper TypedArray types (Uint8Array, Float32Array, etc.)
|
|
313
|
+
4. **Memory**: Clean up large ArrayBuffers when done
|
|
314
|
+
5. **Validation**: Validate data size and format on reception
|
|
315
|
+
|
|
316
|
+
## Future Enhancements
|
|
317
|
+
|
|
318
|
+
Potential improvements for binary message support:
|
|
319
|
+
|
|
320
|
+
- Automatic chunking for large data
|
|
321
|
+
- Built-in compression
|
|
322
|
+
- Progress callbacks
|
|
323
|
+
- Data streaming API
|
|
324
|
+
- Binary message encryption
|
|
325
|
+
- Reliability layer with ACKs
|
|
326
|
+
|
|
327
|
+
## Related Documentation
|
|
328
|
+
|
|
329
|
+
- [API Documentation](./API_DOCUMENTATION.md)
|
|
330
|
+
- [WebRTC Data Channels](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel)
|
|
331
|
+
- [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
|
|
332
|
+
- [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
|