Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mesh sync (LAN)

Feature: net-sync. Peer-to-peer replication over TCP on the local network, with mDNS discovery. No server, no accounts — whoever holds the room key syncs.

use hakodb::net_sync::NetSyncer;
use std::sync::Arc;

let syncer = NetSyncer::new(db.clone(), "node-1", "shared-room-secret", vec![]);
syncer.start(7070).await?;                    // listen + discover + mesh
let status = syncer.status();                 // peer count, known peers
syncer.stop();

How it works:

  1. Discovery: mDNS announcements on the LAN (desktop) or UDP subnet broadcast (Android, which needs no multicast lock). New peers are dialed automatically; known addresses are retried every 15s.
  2. Handshake: Identify (room hash check) + capability exchange (encryption fingerprints — see Encryption posture).
  3. Converge: version maps compared per collection; the ahead side sends deltas; SyncRequest triggers a full bootstrap for newcomers.
  4. Live: WAL tailers stream new ops; relay fan-out covers multi-hop topologies.

Framing is length-prefix + bincode, fail-closed at a 256MB frame cap.

Troubleshooting

SymptomLikely causeFix
Peers never appearmDNS blocked (VLAN, firewall, Docker bridge)Same subnet; open UDP 5353; or seed addresses for direct dial
Connects but no data flowsRoom key mismatch (different rooms, silently isolated)Same key on every node
Android hears nothingDoze / no foreground service; desktop in mDNS-only modeForeground service on Android; Both discovery on desktop for broadcast peers
Encrypted room stalledKeyless/wrong-key/old peer in the roomDistribute the key; upgrade old peers
Screen-off stalls (mobile)OS suspends socketsForeground service; this is the OS, not the engine

Common mistakes

  • Deploying mesh across the internet. It is a LAN protocol. Across networks, run cloud instead.
  • One room key for dev and prod on the same LAN. They will happily merge into one room. Separate keys per environment.