feat: WebRTC Peer-to-Peer Mesh for Offline Field Device Sync (#65)#75
Merged
elizabetheonoja-art merged 1 commit intoJun 24, 2026
Conversation
Utility-Protocol#65) - Implement Lamport vector clock with tick/merge/compare for CRDT conflict resolution - Build SyncPeer class with RTCPeerConnection lifecycle, SCTP data channel, message framing - Build SyncMesh manager with spanning tree routing for >20 peers - Build SignalingClient with WebSocket-based SDP/ICE exchange and reconnection - Build QRDiscovery utility for QR-code fallback manual SDP exchange - Create syncStore as lightweight reactive state store for peers/outbox/vector clocks - Implement useOfflineSync hook orchestrating mesh, signaling, and QR fallback - Wire SDP offers, answers, and ICE candidates through the signaling relay - Add 10 unit tests for VectorClock (tick, merge, compare, serialization, clone)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements Issue #65: WebRTC Peer-to-Peer Mesh for Offline Field Device Sync with CRDT Merge and QR Fallback Discovery.
Changes
Vector Clock (CRDT):
src/utils/vectorClock.ts— Lamport vector clock with tick(), merge(), compare() (Before/After/Concurrent), JSON serialization, and cloneWebRTC Core:
src/services/webrtc/SyncPeer.ts— Manages a single RTCPeerConnection lifecycle, SCTP data channel, message framing/chunking (14KB safe limit), reconnection with exponential backoff. Emits SDP offers and ICE candidates for signaling relay.src/services/webrtc/SyncMesh.ts— Mesh manager with Map<peerId, SyncPeer>. Full mesh for ≤20 peers, minimum spanning tree routing (latency-based) for >20 peers. Relays SDP/ICE through MeshEvent.Signaling:
src/services/webrtc/SignalingClient.ts— WebSocket signaling client at wss://signal.utilityprotocol.com. Exchanges SDP offers/answers and ICE candidates with reconnection.src/services/webrtc/QRDiscovery.ts— QR code fallback for offline discovery. Generates PNG data URLs via qrcode library, decodes scanned payloads with FNV-1a checksum validation.State Management:
src/store/syncStore.ts— Lightweight reactive sync state store (no Redux dependency). Tracks peers, outbox queue, and per-peer vector clocks with subscription pattern.React Hook:
src/hooks/useOfflineSync.ts— Orchestrates the full sync flow: initializes mesh, connects signaling, wires SDP answer/ICE relay, processes incoming entries via CRDT merge, supports QR fallback for offline mode.Tests:
tests/unit/vectorClock.test.ts— 10 unit tests: tick, merge commutativity, Before/After/Concurrent ordering, empty clocks, JSON round-trip, clone independenceCloses #65