v4.0.0
Major redesign of the reconnection and termination semantics.
Reconnection policy
- maxRetries defaults to Infinity (was 3) and counts consecutive failed attempts: the counter resets only after a connection stays open for stableTimeout (new option, default 3s). A server that accepts and immediately drops connections can no longer bypass the limit. (1e4e2cc, fdc41ac)
- The default backoff gained equal jitter (delay/2 + random(delay/2), capped at 10s). (fdc41ac)
- New shouldReconnect(closeEvent, attempt) option — return false to stop reconnecting, terminating with code RECONNECTION_DECLINED. Not consulted for close()/reconnect() calls. (89fc695)
- Errors thrown by url/protocols factories count as failed attempts and follow the normal retry flow (was: instant permanent termination). (cacd11f)
- While reconnecting, readyState is CONNECTING (was CLOSED between attempts); CLOSED now always means permanently terminated, and CLOSING is never reported. (ad09cc9)
Termination
- terminationSignal is the single termination API — the terminate event, isTerminated, and terminationReason are removed. The abort reason is always a ReconnectingWebSocketError with a code. (4ec19e3)
- The signal aborts before the final close event (in v3 the order varied by termination path). (fba5b8f)
close() and reconnect()
- close(code?, reason?) is strictly WHATWG and always terminates permanently. (3992b90)
- The permanently boolean argument is replaced by reconnect(code?, reason?) — drops the current connection and reconnects immediately, not counted towards maxRetries. (3992b90)
Drop-in fidelity
- url returns the configured URL immediately for static URLs (was "" until the first connection). (d8a5f6b)
- binaryType set before the first connection applies to the first connection. (5597855)
- Reassigning onmessage/onclose/etc. keeps the handler's position in the listener order, per spec. (d8a5f6b)
- bufferedAmount includes messages buffered while disconnected. (25f68bb)
- The offline buffer is flushed after the open event, so messages sent from an open listener (e.g. authentication) go out first. (2f502ed)
- Close code 0, reported by some runtimes on connection failure, is normalized to 1006. (d8a5f6b)
React Native
- Fixed a listener leak and unhandled-rejection spam caused by React Native's AbortController polyfill. (3524df6)
- The README now also covers the AbortController polyfill needed for terminationSignal.reason. (e8b4d19)
Removed
- The custom WebSocket constructor option — sockets are always created from globalThis.WebSocket. (b09f1d3)
Full Changelog: v3.0.0...v4.0.0