Peer-equal P2P clock synchronization for Apple devices. No server. No master. ±2ms precision.
No way to synchronize clocks between iPhones on a local network. NTP libraries need an internet server. P2P frameworks don't do clock sync. Multi-device apps are stuck with ±100ms guesswork.
All devices agree on "now" within ±2ms. No internet. No server. No master device. Every peer runs the same 5 lines of code.
NTP-inspired 4-timestamp exchange with best-half filtering across 40 measurements
No master, no slave. Coordinator auto-elected by smallest UUID, invisible to your app
Send/broadcast any command. PeerClock routes it — your app defines the semantics
Push/pull peer status with debounce and generation-based deduplication
Schedule actions to fire simultaneously on all devices with ±2ms precision
3-state connection health: connected → degraded → disconnected
Automatic WiFi → MultipeerConnectivity fallback when network changes
Pure Swift. Foundation + Network.framework only. No third-party libraries
dependencies: [
.package(url: "https://github.com/hakaru/PeerClock.git", from: "0.2.0")
]
import PeerClock
let clock = PeerClock()
try await clock.start()
// Synchronized time — agrees across all devices ±2ms
let timestamp = clock.now
// Broadcast to all peers
try await clock.broadcast(
Command(type: "com.myapp.record.start")
)
// Receive
for await (sender, command) in clock.commands {
handleCommand(command, from: sender)
}
// Fire on all devices at the same time (±2ms)
let fireTime = clock.now + 3_000_000_000 // 3 seconds from now
let handle = try await clock.schedule(atSyncedTime: fireTime) {
startRecording()
}
PeerClock uses an NTP-inspired 4-timestamp protocol:
| Error Source | Raw Error | Mitigation | Residual |
|---|---|---|---|
| Wi-Fi UDP jitter | 1–10ms | Best-half filtering | ~1–2ms |
| Crystal oscillator drift | 50ppm | Periodic re-sync | <0.25ms |
| iOS scheduling | <1ms | mach_continuous_time | <1ms |
| Total | ±2ms |
Start recording on multiple iPhones simultaneously with sample-accurate alignment
Synchronize timecode across devices for post-production editing
Play audio/video in perfect sync across a room of devices
Monitor battery, storage, and state across connected devices in real-time
Open source, zero dependencies, MIT licensed. 127 tests passing.