Handling Edge Cases

1. Handling Simultaneous Connections

CaseStrategy
Multi-tabAllow N; share via BroadcastChannel
Same device repeat connectReplace older or limit N
Concurrent loginClose oldest session

2. Handling Connection Migration

TriggerDetail
Network switch (Wi-Fi → cell)Reconnect with resume token
IP changeRe-handshake
VPN connectForce close + reconnect

3. Handling Server Restart

Server Restart Sequence

  1. Server SIGTERM → stop accepting
  2. Broadcast close 1001 with retry hint
  3. Drain in-flight messages (timeout)
  4. Exit process; new version starts
  5. Clients reconnect with jitter

4. Handling Clock Skew

MitigationDetail
Server timestampsAuthoritative time on messages
Skew estimate(serverTs - clientTs) average
JWT leeway±30s on exp/nbf
NTP syncServer clocks via chrony/ntpd

5. Handling Message Reordering

SourceMitigation
Single socketTCP guarantees order
ReconnectSequence number + buffer
Pub/sub fan-outOrder per topic; not cross-topic

6. Handling Partial Messages

Note: Browsers always deliver complete WebSocket messages (reassembled from fragments). Application-level partial messages must be modeled explicitly with chunk IDs.
LayerBehavior
Protocol fragmentationAuto-reassembled
App chunkingReassemble by id/seq
Streaming API (WebSocketStream)True streaming chunks

7. Handling Unicode Characters

IssueDetail
Surrogate pairsUse String.prototype[Symbol.iterator] for char count
Normalizationstr.normalize("NFC")
Byte lengthnew TextEncoder().encode(str).length
Bad UTF-8Server closes 1007

8. Handling Empty Messages

FrameTreatment
Empty textValid; e.data === ""
Empty binaryValid; zero-length buffer
App schemaReject if required field absent

9. Handling Connection Limits

LayerLimit
Browser per-originTypically 200+ (no strict cap)
OS file descriptorsulimit -n (raise to 65535+)
Ephemeral ports~28k per src IP
LBPer-target / per-listener caps

10. Handling Memory Leaks

Common LeakFix
Closure captures bufferNull reference on close
Pending RPC mapReject on close + clear
Listener accumulationAbortController
Subscriber registryCleanup on disconnect