peerpigeon
Version:
WebRTC-based peer-to-peer mesh networking library with intelligent routing and signaling server
248 lines (182 loc) • 4.52 kB
Markdown
A local WebSocket server for PeerPigeon development and testing.
- WebSocket-based peer signaling
- XOR distance-based peer discovery
- WebRTC signaling message routing
- In-memory peer storage
- Real-time peer announcements
- Graceful connection handling
```bash
npm install peerpigeon
npm start
PORT=8080 HOST=0.0.0.0 npm start
```
```bash
git clone https://github.com/draeder/peerpigeon.git
cd peerpigeon
npm start
```
```javascript
import { PeerPigeonServer } from 'peerpigeon';
// Create and configure server
const server = new PeerPigeonServer({
port: 3000,
maxConnections: 500
});
// Start server
await server.start();
// Listen for events
server.on('peerConnected', ({ peerId, totalConnections }) => {
console.log(`New peer connected: ${peerId}`);
});
```
The server will start on `ws://localhost:3000` by default.
Update your browser application to connect to the local server:
```javascript
const mesh = new PeerPigeonMesh();
await mesh.connect('ws://localhost:3000');
```
- `PORT`: Server port (default: 3000)
- `HOST`: Server host (default: localhost)
### Example Usage
```bash
# Start on custom port
PORT=8080 npm start
HOST=0.0.0.0 npm start
```
Connect with a valid 40-character hex peer ID:
```
ws://localhost:3000?peerId=abc123def456...
```
```json
{
"type": "announce",
"data": { "peerId": "abc123..." }
}
```
```json
{
"type": "offer",
"data": { "sdp": "..." },
"targetPeerId": "def456..."
}
```
```json
{
"type": "ping",
"data": { "timestamp": 1234567890 }
}
```
```json
{
"type": "connected",
"peerId": "abc123...",
"timestamp": 1234567890
}
```
```json
{
"type": "peer-discovered",
"data": { "peerId": "def456..." },
"fromPeerId": "system",
"targetPeerId": "abc123...",
"timestamp": 1234567890
}
```
```json
{
"type": "pong",
"timestamp": 1234567890,
"originalTimestamp": 1234567885
}
```
You can test the server using a WebSocket client:
```javascript
const ws = new WebSocket('ws://localhost:3000?peerId=1234567890abcdef1234567890abcdef12345678');
ws.onopen = () => {
console.log('Connected');
// Send announcement
ws.send(JSON.stringify({
type: 'announce',
data: { peerId: '1234567890abcdef1234567890abcdef12345678' }
}));
};
ws.onmessage = (event) => {
console.log('Received:', JSON.parse(event.data));
};
```
1. Open `examples/browser/index.html` in multiple browser tabs
2. Update the WebSocket URL to `ws://localhost:3000`
3. Click "Connect" in each tab
4. Watch peers discover each other
For development with automatic restarts:
```bash
npm run dev
```
The server provides detailed logging:
- ✅ Peer connections
- 📨 Message routing
- 📢 Peer announcements
- 🔌 Disconnections
- ❌ Errors
```
Browser Client 1 Browser Client 2 Browser Client N
| | |
v v v
WebSocket WebSocket WebSocket
| | |
+-------------------+-------------------+
|
Local WebSocket Server
|
In-Memory Storage
(peers, connections)
```
## Troubleshooting
### Common Issues
1. **Connection Refused**: Check if server is running on correct port
2. **Invalid Peer ID**: Ensure peer ID is 40-character hex string
3. **Message Not Delivered**: Check if target peer is connected
4. **Server Won't Start**: Check if port is already in use
### Debug Commands
```bash
# Check if port is in use
lsof -i :3000
# Kill process on port
kill -9 $(lsof -t -i:3000)
# Check server logs
npm start | grep "ERROR"
```
## License
MIT - Same as PeerPigeon main project