Skip to main content

Overview

Loading a Digital Twin follows a strict sequence: inject the SDK script, initialize with credentials, poll for readiness, subscribe to events, then start the conversation. Skipping or reordering these steps causes silent failures. This guide walks through each phase using the sdk-sample.js reference implementation.

Phase 1: Script Injection

Wait for the DOM, then dynamically load the SDK script from your Pria server.
You can inject optional CSS before loading the script to constrain the Pria UI dimensions:

Phase 2: SDK Initialization

Inside the script.onload callback, call window.priasdk() with five arguments. This creates the Pria iframe and performs the launch handshake with the Pria middleware; the embedded app then establishes its realtime connection.
openOnLoad: false is critical. Without it, the user sees a blank chat panel while the backend is still connecting. The UI should only appear after convo.start succeeds.

Persistent User Identity

The reference implementation uses cookies to maintain a stable user identity across visits, which preserves conversation history:

Phase 3: Poll for Readiness

The SDK needs time to load the iframe and complete the launch handshake with the Pria middleware. Poll isReady() until it returns true.
isReady() returns true only after the iframe is in place and the launch handshake with the middleware has completed. Never send commands before this returns true.

Phase 4: Subscribe and Setup

Once the SDK is ready, subscribe to response events and wire up your UI trigger.
The button click triggers four actions in sequence:
  1. Hide the Pria UI — prevents showing an empty chat window
  2. Show a loading overlay — gives the user visual feedback
  3. Send convo.start — tells the backend to initialize the AI session

Phase 5: Response Handling with Retry

The response handler manages two scenarios: successful start and transient connection errors.

How the Retry Loop Works

The NO-SESSION error means the Socket.io connection is established but the AI backend session hasn’t initialized yet. This is a normal race condition — retrying resolves it within 1-2 attempts.

Loading Overlay

Provide visual feedback while the Digital Twin connects. The reference implementation uses a full-screen overlay with a spinner:

Complete Minimal Example

Putting it all together — a minimal working integration:

Common Pitfalls

Sending commands before isReady()If you call pria.send() before the SDK is ready, the command throws an error or is silently dropped. Always gate on isReady().
Missing subscribe() before convo.startIf you send convo.start without subscribing first, you won’t receive the success response and your UI will never transition from the loading state.
Not handling page unloadCall pria.destroy() on beforeunload to cleanly disconnect the Socket.io session. Without this, the server may hold stale sessions.