Optimizing for Mobile

1. Handling Mobile Network Issues

IssueMitigation
High RTT (cell)Tolerate; batch messages
NAT rebindingAggressive heartbeat (15-30s)
Carrier proxiesUse wss:// (port 443)
Network switchReconnect with resume
Packet lossApp ack + retransmit

2. Implementing Background Connection

PlatformStrategy
iOSAPNS push wakes app; reconnect on resume
AndroidFCM push or foreground service
Web (mobile)Hidden tab pauses timers; close on hide

3. Reducing Battery Consumption

TacticDetail
Lower heartbeat when idle2 min vs 30s
Coalesce updatesSingle radio wake-up
Disconnect on hideReconnect on resume
Push for important onlyNative push for events that need wake

4. Handling App Suspension

Example: visibilitychange

document.addEventListener("visibilitychange", () => {
  if (document.hidden) ws?.close(4001, "background");
  else reconnect();
});
EventAction
visibilitychange (hidden)Close or downgrade
pageshow (BFCache restore)Reconnect
freeze / resumeNative lifecycle

5. Optimizing for Slow Networks

OptimDetail
Small messages< 1 KB
Binary formatmsgpack/protobuf
Compressionpermessage-deflate
Skip non-criticalDrop telemetry on 2G
Larger backoffMultiplied on slow net

6. Implementing Offline Support

CapabilityDetail
Local queueIndexedDB persistence
Optimistic UIShow local, reconcile
Idempotent opsClient-id deduplication
Conflict resolutionCRDT or server-wins

7. Handling Network Type Changes

APIUse
navigator.connection.effectiveType"4g","3g","2g","slow-2g"
change eventAdapt heartbeat / batch size
saveData flagReduce traffic

8. Using Page Visibility API

StateAction
hiddenPause heartbeat, reduce updates
visibleReconnect if needed, fetch missed
prerenderSkip connect until visible

9. Testing on Mobile Devices

ToolUse
Chrome remote debugAndroid USB
Safari Web InspectoriOS USB
DevTools throttlingSimulate 3G
Real-device labsBrowserStack, Sauce

10. Monitoring Mobile Performance

MetricDetail
Reconnect rateMobile baseline higher
RTT histogramBy network type
Battery (native)Power profiler in OS
Data usageBytes per session