Files
smart-org/apps
mjalmoudy94 e50c86c451 fix(web): stop authenticating WebSocket reconnect storm
Console evidence: on the authenticated layout, api.me() opened
wss://.../api/ws?token=<JWT> and the browser logged "WebSocket is closed
before the connection is established", then an uncaught
connection_closed rejection — a self-inflicted reconnect storm.

Root cause (two halves):
1. connectedToken was only assigned inside onopen, so during the CONNECTING
   window every other caller (RealtimeProvider's 2s poll, rpc's whenOpen)
   saw connectedToken !== token and called openWith again, tearing down the
   socket it had just dialled.
2. Replacing a socket left its late onclose to run onSocketClosed, which nulled
   this.socket and connectedToken — clobbering the successor and firing a
   spurious reconnect + rejected openPromise.

Fix:
- Set connectedToken the moment we commit to a dial (in openWith), so
  concurrent callers wait on the same openPromise instead of re-dialling.
- Guard onopen/onmessage/onclose with `this.socket !== socket` so a replaced
  socket's late callbacks are ignored and cannot clobber its successor.

Anonymous login escaped this (both tokens null); an authenticated connection
(a real JWT) triggered it, matching the reported failure.
2026-10-13 16:07:37 +03:30
..