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. It drops the Pria window into your page as an iframe and signs the user in against Pria’s servers (the launch handshake). Once that succeeds, the twin opens its own live connection for streaming replies and voice.
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. Start a safety timer — so the page can recover if the session never comes up (see Phase 5)
  4. 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 person is not signed in to the Digital Twin yet. The SDK now holds commands until the twin is listening, so this error is rare; the retry stays as a backstop and 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 has connected, it throws “Connect Pria first, then retry.” Always gate on isReady(). After that, commands sent before the twin is listening are held (up to 20, for 30 seconds each) and replayed in order — see Knowing when the twin is ready.
Hiding the UI while an admin must confirm it’s themThis sample hides Pria while it connects. When an admin account opens Pria, the panel may ask for a code first (Confirm it’s you), and commands are refused until it is entered. Use the onAuthRequired display option to show the panel — see Confirm it’s you.
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.