Skip to main content
Establishes a WebRTC connection with a real-time model and returns a RealTimeClient instance for controlling the stream.

Method Signature

connect(
  stream: MediaStream | null,
  options: RealTimeClientConnectOptions
): Promise<RealTimeClient>

Parameters

stream
MediaStream | null
required
The local media stream to send to the server. Pass null for models that don’t require input (like live_avatar without microphone input).For camera/microphone input, use navigator.mediaDevices.getUserMedia().
options
RealTimeClientConnectOptions
required
Connection configuration options.

Return Value

RealTimeClient
Promise<RealTimeClient>
A promise that resolves to a RealTimeClient instance with the following methods and properties:

Error Handling

The method throws an error if:
  • Options validation fails (invalid parameters)
  • WebRTC connection fails
  • WebSocket connection fails
  • Initial image/prompt fails to send
try {
  const client = await decart.realtime.connect(stream, options);
} catch (error) {
  if (error instanceof DecartSDKError) {
    console.error('Connection failed:', error.code, error.message);
  }
}

Usage Examples

Basic Connection

import { createDecartClient, models } from '@decart/sdk';

const decart = createDecartClient({ apiKey: 'your-api-key' });

// Get user's camera
const stream = await navigator.mediaDevices.getUserMedia({
  video: true,
  audio: false
});

// Connect to Mirage model
const client = await decart.realtime.connect(stream, {
  model: models.realtime.mirage(),
  onRemoteStream: (remoteStream) => {
    const video = document.getElementById('output');
    video.srcObject = remoteStream;
  }
});

console.log('Connected! Session ID:', client.sessionId);

Connection with Initial State

const client = await decart.realtime.connect(stream, {
  model: models.realtime.lucyV2V720pRt(),
  onRemoteStream: (remoteStream) => {
    videoElement.srcObject = remoteStream;
  },
  initialState: {
    prompt: {
      text: 'Transform into a watercolor painting',
      enhance: true
    }
  }
});

Live Avatar (No Input Stream)

// No input stream required for live_avatar
const client = await decart.realtime.connect(null, {
  model: models.realtime.liveAvatar(),
  onRemoteStream: (remoteStream) => {
    videoElement.srcObject = remoteStream;
  },
  initialState: {
    image: 'https://example.com/avatar.jpg',
    prompt: {
      text: 'Friendly greeting expression'
    }
  }
});

// Play audio through the avatar
await client.playAudio(audioBlob);

Connection with Event Listeners

const client = await decart.realtime.connect(stream, {
  model: models.realtime.mirage(),
  onRemoteStream: (remoteStream) => {
    videoElement.srcObject = remoteStream;
  }
});

// Listen for connection state changes
client.on('connectionChange', (state) => {
  console.log('Connection state:', state);
});

// Listen for errors
client.on('error', (error) => {
  console.error('Error:', error.message);
});

// Listen for generation ticks
client.on('generationTick', ({ seconds }) => {
  console.log('Generating for', seconds, 'seconds');
});

See Also

Build docs developers (and LLMs) love