Lifecycle: Reset & Shutdown

The SDK holds three pieces of mutable state during its lifetime: an in-memory session token, an event queue, and (optionally) a WebSocket connection. Two methods let you wind that state down — one for logging out, one for tearing the SDK down entirely.

reset()

Use when the user changes — typically on logout, but also when an account-switcher signs in as a different user.

treliq.reset();

What it does:

  • Clears the in-memory session token.
  • Clears the cached external user ID and end-user ID.
  • Closes the in-app content WebSocket and cancels reconnect timers.
  • Removes any tq_* keys from localStorage and sessionStorage (defensive — the current SDK doesn’t write any, but a wrapper or future release might).
  • Tears down rendered in-app UI.

After reset(), the SDK is back in its pre-identify() state. The next call to identify() re-establishes a fresh session.

reset() does not touch the API key or project ID — the SDK instance itself remains usable.

shutdown()

Use when the SDK instance is going away — typically on beforeunload, or in a single-page app’s unmount path if you create instances dynamically.

await treliq.shutdown();

What it does:

  • Disables auto-tracking (restores the original history.pushState / replaceState).
  • Stops the event queue’s flush timer.
  • Flushes any remaining queued events to the server. The promise resolves once the flush completes.
  • Tears down the offline online/offline listeners.
  • Closes the WebSocket and clears the in-app render queue.

After shutdown(), do not call any other methods on the instance. Create a new one with init() if you need to start over.

Logout pattern

async function logout() {
  await myAuthClient.signOut();   // your auth library
  treliq.reset();                 // clear Treliq identity
  router.push('/login');
}

reset() is synchronous — no need to await.

Page-unload pattern

window.addEventListener('beforeunload', () => {
  // Fire-and-forget; the browser may not wait for the promise
  treliq.shutdown();
});

For events that absolutely must reach the server before unload, use track(event, props, { immediate: true }) rather than relying on shutdown’s flush.

Multi-tenant pattern

If your app supports multiple Treliq projects in the same page (rare, but possible), give each its own instance:

const consumerSDK = init({ apiKey: 'tk_live_a', projectId: 'p_consumer' });
const adminSDK   = init({ apiKey: 'tk_live_b', projectId: 'p_admin' });

// Tear them down independently
await consumerSDK.shutdown();
await adminSDK.shutdown();