Transport Protocols
WebSocket
WebSocket is the default transport for GUN relay peers. It provides bidirectional, real-time communication between peers.Server Configuration
~/workspace/source/examples/http.js:28-32
WebRTC
WebRTC enables direct peer-to-peer connections between browsers without requiring a relay server. GUN includes built-in WebRTC support through thegun/lib/webrtc.js module.
~/workspace/source/lib/webrtc.js:22-36
How WebRTC Works in GUN
- Peer Discovery: Peers announce themselves to a room using GUN’s graph sync
- Offer/Answer Exchange: SDP offers and answers are exchanged via GUN messages
- ICE Candidates: Connection candidates are shared for NAT traversal
- Data Channel: Once connected, messages flow through the WebRTC data channel
~/workspace/source/lib/webrtc.js:8
Media Streams
GUN’s WebRTC implementation supports media streams for video/audio:~/workspace/source/lib/webrtc.js:52-62
Multicast
Multicast enables UDP-based local network discovery in Node.js environments. This is perfect for IoT devices or local-first applications.~/workspace/source/lib/multicast.js:10-13
Disabling Multicast
~/workspace/source/lib/multicast.js:7
Peer Discovery
Static Peers
Manually specify peer URLs:Dynamic Peer Discovery
GUN automatically discovers peers through:- WebRTC Room Announcements: Peers join rooms and discover each other
- Multicast Broadcasting: Local network peers announce themselves via UDP
- Peer Gossip: Connected peers share information about other peers
AXE Relay Protocol
AXE (Advanced eXchange Engine) is GUN’s intelligent routing protocol that optimizes message delivery across the mesh network.~/workspace/source/lib/axe.js:12
AXE features:
- Smart routing: Messages are routed to subscribed peers only
- Subscription tracking: Peers track which data other peers are interested in
- Load balancing: GET requests are distributed across multiple peers
- Deduplication: Prevents duplicate messages using hash checking
Mesh Architecture
GUN’s mesh networking layer (src/mesh.js) handles all peer communication:
Message Flow
- hear(): Receive messages from peers
- say(): Send messages to peers
- Batching: Messages are batched for efficient network utilization
- Deduplication: Duplicate messages are filtered using message IDs
~/workspace/source/src/mesh.js:24-194
Peer States
~/workspace/source/src/mesh.js:266-294
Message Limits
~/workspace/source/src/mesh.js:14-17
Network Optimization
Connection Pooling
GUN maintains a pool of peer connections and automatically reconnects on failure.Backpressure Handling
Messages are queued when peers are temporarily unavailable:~/workspace/source/src/mesh.js:262
Message Batching
Messages are automatically batched into JSON arrays to reduce network overhead:~/workspace/source/src/mesh.js:178-192
Security Considerations
HTTPS/WSS
For production deployments, always use encrypted connections:~/workspace/source/examples/http.js:18-22
Peer Authentication
Use GUN’s SEA (Security, Encryption, Authorization) for peer authentication:Debugging Network Issues
Enable Statistics
~/workspace/source/src/mesh.js:328
Message Tracking
Enable debug logging to track message flow:Best Practices
- Use relay peers for production: Browser-to-browser WebRTC should be supplemented with relay peers for reliability
- Configure STUN/TURN servers: Required for WebRTC NAT traversal
- Set reasonable connection limits: Default WebRTC max is 55 connections
- Enable multicast for local networks: Great for offline-first IoT applications
- Monitor peer connections: Track hi/bye events to understand network topology
- Use AXE for large meshes: Improves routing efficiency with many peers
- Batch writes: Group multiple puts to reduce network messages
Next Steps
- Learn about Conflict Resolution in distributed networks
- Optimize Performance for your use case
- Understand CAP Theorem tradeoffs
- Deploy to Production