Skip to content

feat: WebRTC Peer-to-Peer Mesh for Offline Field Device Sync (#65)#75

Merged
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
Kami-no-san:fix/issue-65-webrtc-p2p-mesh
Jun 24, 2026
Merged

feat: WebRTC Peer-to-Peer Mesh for Offline Field Device Sync (#65)#75
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
Kami-no-san:fix/issue-65-webrtc-p2p-mesh

Conversation

@Kami-no-san

Copy link
Copy Markdown
Contributor

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 clone

WebRTC 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 independence

Closes #65

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)

@elizabetheonoja-art elizabetheonoja-art left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@elizabetheonoja-art elizabetheonoja-art merged commit e9f9077 into Utility-Protocol:main Jun 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebRTC Peer-to-Peer Mesh for Offline Field Device Sync with CRDT Merge and QR Fallback Discovery

2 participants