When a user opens your application in several browser tabs at the same time, each tab normally creates its own WebSocket connection to the XMPP server. The Strophe.js shared-worker transport avoids this by running a singleDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/strophe/strophejs/llms.txt
Use this file to discover all available pages before exploring further.
WebSocket inside a SharedWorker and multiplexing all tabs through it. Every Connection instance points its WorkerWebsocket transport at the shared worker script, which acts as a ConnectionManager that fans out incoming messages to all connected ports.
How It Works
The architecture has three layers:Connection(main thread) — the regular Strophe.js connection object your application code interacts with.WorkerWebsocket(main thread) — a subclass ofWebsocketthat, instead of opening a rawWebSocket, opens aSharedWorkerand communicates with it overMessagePort.shared-connection-worker.ts(worker thread) — theConnectionManagerclass that owns the single realWebSocket, forwards all messages to every registered port, and accepts send/close commands from any port.
Setup
Build or copy the worker script
The shared worker script is located at
src/shared-connection-worker.ts in the Strophe.js source tree. Build your project so that it is available as a standalone browser-compatible JavaScript file at a URL your application can reference.Pass the `worker` option to the Connection constructor
Set the
worker option to the URL of the shared worker script. Strophe.js detects the presence of this option and automatically selects WorkerWebsocket as the transport, regardless of the service URL scheme.Connect normally
From this point the API is identical to a regular WebSocket connection. Call
connection.connect() from each tab; the worker handles opening the underlying socket only once.Handle tab attach / reattach
When a second tab opens after the first has already authenticated, the
WorkerWebsocket sends an _attach command to the worker. The ConnectionManager checks whether the socket is still open and responds with either ATTACHED or ATTACHFAIL. The second tab’s WorkerWebsocket._attachCallback() translates this into the appropriate Strophe.Status for your callback.Full Example
WorkerWebsocket vs. Websocket
WorkerWebsocket extends Websocket and overrides the parts that deal with the raw socket:
| Aspect | Websocket | WorkerWebsocket |
|---|---|---|
| Socket owner | Main thread | ConnectionManager inside SharedWorker |
_connect() | Opens new WebSocket(...) | Posts ['_connect', service, jid] to the worker port |
_disconnect() | Sends <close/> then closes socket | Posts <close/> string to the worker port |
_onOpen / _onClose / _onMessage | Bound directly to the socket | Forwarded via MessagePort events from the worker |
_replaceMessageHandler() | Swaps socket.onmessage | Swaps an internal _messageHandler reference |
ConnectionManager in the worker script keeps a ports array. Every time a new tab connects, its port is added to the array. Incoming WebSocket messages are broadcast to all ports; outgoing sends go directly from each port to the socket.
Browser Compatibility
To handle environments whereSharedWorker is unavailable, check for its existence before using the worker option and fall back to a regular WebSocket connection:
The shared-worker transport only works with WebSocket endpoints. Passing a BOSH (
http:// or https://) URL together with the worker option is not supported. The worker option takes precedence over the URL scheme — Strophe.js uses WorkerWebsocket whenever worker is set.