SharedWorker usage and disposal guide
This guide explains how the ThrillConnect SharedWorker mode behaves, how consumers should use it, and how to dispose clients safely.
How SharedWorker mode is selected
ThrillConnect uses SharedWorker when both conditions are met:
window.SharedWorkerexists in the runtime.disable_workeris not set totrueinConnectSettings.
Otherwise, the client falls back to the local per-tab implementation.
Correct consumer usage patterns
1) Instantiate once per tab/window
Create one ThrillConnect instance per page context and reuse it across your UI code.
const client = new ThrillConnect({
host: "localhost:11000",
brand: { operator_id: "thrilltech", brand_id: "brand1" },
events: [ConnectEvents.JackpotUpdateEvent, ConnectEvents.OptInEvent],
})
2) Authenticate explicitly when credentials become available or rotate
In SharedWorker mode, authenticate updates credentials in the shared client state and is used on reconnection attempts.
client.authenticate({
id: "player_00001",
token: "token_00001",
country: "USA",
currency: "USD",
})
Use this whenever a tab refreshes user context (SSO refresh, OTT rotation, relogin).
3) Subscribe and unsubscribe intentionally
- Register only the events you actually need in
settings.events. - Keep handler references if you want to remove a specific listener later.
- Use
off(event)to clear all handlers for one event type.
4) Prefer SharedWorker only for single-session multi-tab use cases
SharedWorker is ideal when all tabs/windows represent the same player session.
If your app supports different logged-in users across tabs, disable shared worker:
const client = new ThrillConnect({
// ...settings
disable_worker: true,
})
Disposal guide
Why disposal matters
A client instance keeps event subscriptions and an open port to the shared worker. Disposing on page teardown prevents stale listeners and releases the port early.
Recommended disposal pattern
Always call destroy() when the page/app is unloading:
const cleanup = () => client.destroy()
window.addEventListener("beforeunload", cleanup)
In SPA frameworks, call destroy() in component/app unmount lifecycle hooks too.
Disposal semantics in SharedWorker mode
When destroy() is called from a consumer:
- The tab posts
closeto the shared worker. - The tab closes its local worker port.
- The tab should no longer use that client instance.
Inside the worker, once all ports are gone, the underlying shared ThrillConnectLocal is destroyed.
Quick checklist
- One client instance per tab/window context.
-
Call
authenticatewhenever credentials update. - Subscribe only to required events.
- Unsubscribe handlers you no longer need.
-
Call
destroy()on unload/unmount. -
Set
disable_worker: truefor multi-user-per-browser scenarios.