Handling Edge Cases
1. Handling Simultaneous Connections
| Case | Strategy |
|---|---|
| Multi-tab | Allow N; share via BroadcastChannel |
| Same device repeat connect | Replace older or limit N |
| Concurrent login | Close oldest session |
2. Handling Connection Migration
| Trigger | Detail |
|---|---|
| Network switch (Wi-Fi → cell) | Reconnect with resume token |
| IP change | Re-handshake |
| VPN connect | Force close + reconnect |
3. Handling Server Restart
Server Restart Sequence
- Server SIGTERM → stop accepting
- Broadcast close 1001 with retry hint
- Drain in-flight messages (timeout)
- Exit process; new version starts
- Clients reconnect with jitter
4. Handling Clock Skew
| Mitigation | Detail |
|---|---|
| Server timestamps | Authoritative time on messages |
| Skew estimate | (serverTs - clientTs) average |
| JWT leeway | ±30s on exp/nbf |
| NTP sync | Server clocks via chrony/ntpd |
5. Handling Message Reordering
| Source | Mitigation |
|---|---|
| Single socket | TCP guarantees order |
| Reconnect | Sequence number + buffer |
| Pub/sub fan-out | Order 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.
| Layer | Behavior |
|---|---|
| Protocol fragmentation | Auto-reassembled |
| App chunking | Reassemble by id/seq |
| Streaming API (WebSocketStream) | True streaming chunks |
7. Handling Unicode Characters
| Issue | Detail |
|---|---|
| Surrogate pairs | Use String.prototype[Symbol.iterator] for char count |
| Normalization | str.normalize("NFC") |
| Byte length | new TextEncoder().encode(str).length |
| Bad UTF-8 | Server closes 1007 |
8. Handling Empty Messages
| Frame | Treatment |
|---|---|
| Empty text | Valid; e.data === "" |
| Empty binary | Valid; zero-length buffer |
| App schema | Reject if required field absent |
9. Handling Connection Limits
| Layer | Limit |
|---|---|
| Browser per-origin | Typically 200+ (no strict cap) |
| OS file descriptors | ulimit -n (raise to 65535+) |
| Ephemeral ports | ~28k per src IP |
| LB | Per-target / per-listener caps |
10. Handling Memory Leaks
| Common Leak | Fix |
|---|---|
| Closure captures buffer | Null reference on close |
| Pending RPC map | Reject on close + clear |
| Listener accumulation | AbortController |
| Subscriber registry | Cleanup on disconnect |