SyncTransport) that abstracts the communication mechanism between the POS frontend and the sync backend. Two implementations exist:
directTransport— HTTP polling + localStorage outbox (existing behavior, no hub needed)hubTransport— WebSocket connection to the hub server
SyncTransport interface
Defined innu_pos_react/src/realtime/transport.ts:
on* methods return an unsubscribe function.
Transport states
Factory: createTransport()
nu_pos_react/src/realtime/createTransport.ts:
nu_hub_enabled(boolean) — Master switchnu_hub_url(string) — WebSocket URL (e.g.,ws://10.0.0.5:8766)nu_hub_fallback_ip(string) — IP-only fallback, auto-prefixed withws://and port8766
Direct transport
nu_pos_react/src/realtime/directTransport.ts
Wraps the existing sync modules with no behavior change:
Hub transport
nu_pos_react/src/realtime/hubTransport.ts
Lease tracking
heldLeases: Set<string>— Order references currently leased by this terminalpendingLeases: Map<string, { resolve, timer }>— In-flight acquire requests- Heartbeat timer: sends
LEASE_HEARTBEATfor all held leases every 20s
Table lock tracking
heldTableLocks: Set<number>— Table IDs currently locked by this terminalpendingTableLocks: Map<number, { resolve, timer }>— In-flight lock requests- Separate heartbeat timer: sends
TABLE_LOCK_HEARTBEATfor all held locks every 15s
Offline fallback
Whenconnection.send() returns false (WS not open), sendOrderSnapshot() queues the snapshot to the localStorage outbox:
Reconnection events
The hub transport firesonReconnecting and onReconnected callbacks after the initial connection has succeeded at least once (hasConnectedBefore flag). These are wired through to the SyncUIProvider to show a full-screen reconnection overlay.
The direct transport provides no-op stubs since it has no WebSocket lifecycle.
Token refresh on AUTH_FAIL
HubTransportConfig uses a dynamic token getter instead of a static token:
AUTH_FAIL, the connection layer calls onTokenExpired() to attempt a JWT refresh. If the refresh succeeds, it reconnects with the new token (via getToken()). If the refresh token is also expired, it fires onAuthFailed for a permanent logout redirect.
A tokenRefreshAttempted flag prevents infinite refresh loops within a single connection cycle.
Visibility change detection
hubConnection.ts listens to document.visibilitychange events. When a tab regains focus after laptop sleep, the browser often leaves the WebSocket in a stale state (TCP connection dead but readyState not updated). The visibility handler force-closes dead sockets to trigger an immediate reconnect instead of waiting 30-90s for the TCP timeout.
Connection state mapping
Hub discovery
nu_pos_react/src/realtime/hub/hubDiscovery.ts
Resolution priority:
nu_hub_urlfrom bootstrap — used as-is if starts withws://orwss://, otherwise prefixednu_hub_fallback_ipfrom bootstrap — prefixed withws://and:8766null— hub mode disabled, factory returns direct transport