Simli WebRTC Client
SimliClient is a WebRTC frontend client designed for real-time interaction with AI-powered facial recognition and avatar services. It enables developers to integrate live audio and video streaming capabilities into their web applications, specifically for scenarios involving AI-driven avatars or face-based interactions. The current stable version is 3.0.1. While a specific release cadence isn't explicitly stated, the package follows semantic versioning, with major version changes (like the jump to v3) indicating significant updates and potential breaking changes. Its primary differentiator is its focus on streamlining the integration of WebRTC with Simli's AI backend for facial analysis and avatar control, abstracting much of the low-level WebRTC API complexity for these specialized use cases.
Common errors
-
TypeError: SimliClient is not a constructor
cause Attempting to import SimliClient using CommonJS 'require' syntax in an ESM-only or hybrid environment where the default export is not correctly handled.fixChange your import statement to `import { SimliClient } from 'simli-client';` for ES Module compatibility. -
Error: API key is missing or invalid.
cause The `apiKey` field in the `SimliClientConfig` object was either not provided or contained an empty/invalid string.fixEnsure `SimliClientConfig.apiKey` is set to a valid, non-empty API key provided by Simli. Double-check for typos. -
SimliClient: Connection failed: Signaling error occurred.
cause The WebRTC signaling process failed, often due to an unreachable `serverUrl`, incorrect server configuration, or network issues preventing the client from negotiating a connection with the Simli backend.fixVerify that `SimliClientConfig.serverUrl` is correct and accessible. Check firewall rules, network connectivity, and the status of your Simli backend server. Inspect console logs for more specific WebSocket or WebRTC errors. -
ReferenceError: navigator is not defined
cause Attempting to use browser-specific APIs like `navigator.mediaDevices.getUserMedia` in a Node.js environment without appropriate polyfills or mocking.fixEnsure your code runs in a browser environment or use a tool like JSDOM to mock browser APIs if running unit tests in Node.js. For server-side operations, SimliClient is not typically used directly.
Warnings
- breaking The jump from version 2.x to 3.x likely introduced breaking changes in API signatures, configuration options, or internal mechanisms. Developers upgrading from previous major versions should consult the official documentation for migration guides.
- gotcha API keys and sensitive server URLs should never be directly hardcoded into client-side bundles for production applications. Exposure can lead to unauthorized access and abuse of your Simli services.
- gotcha WebRTC connections can be sensitive to network environments, including firewalls, NAT traversal, and network latency. Users behind restrictive networks may experience connection failures or poor performance.
- gotcha The `Initialize` and `start` methods are asynchronous. Calling other methods before these operations complete can lead to runtime errors or unexpected behavior as the WebRTC connection might not be ready.
Install
-
npm install simli-client -
yarn add simli-client -
pnpm add simli-client
Imports
- SimliClient
const SimliClient = require('simli-client');import { SimliClient } from 'simli-client'; - SimliClientConfig
import type { SimliClientConfig } from 'simli-client'; - Initialize
await client.Initialize(config);
Quickstart
import { SimliClient } from 'simli-client';
import type { SimliClientConfig } from 'simli-client';
async function setupSimliConnection() {
// In a real application, ensure these values are loaded securely (e.g., environment variables)
const SIMLI_API_KEY = process.env.SIMLI_API_KEY ?? 'your-simli-api-key-here';
const SIMLI_SERVER_URL = process.env.SIMLI_SERVER_URL ?? 'wss://your.simli.server.com/ws';
if (SIMLI_API_KEY === 'your-simli-api-key-here') {
console.warn("WARNING: Please replace 'your-simli-api-key-here' with your actual Simli API key.");
}
if (SIMLI_SERVER_URL === 'wss://your.simli.server.com/ws') {
console.warn("WARNING: Please replace 'your.simli.server.com' with your actual Simli server URL.");
}
const config: SimliClientConfig = {
apiKey: SIMLI_API_KEY,
serverUrl: SIMLI_SERVER_URL,
// Optional: avatarId: 'default-avatar',
// Optional: logLevel: 'info' // 'debug', 'warn', 'error'
};
const client = new SimliClient();
// Attach event listeners for connection status
client.on('connected', () => {
console.log('SimliClient: Successfully connected to the server.');
// You can start sending data here
// For demonstration, let's send dummy audio for 5 seconds
const dummyAudio = new Uint8Array(1024).fill(0);
let sendCount = 0;
const interval = setInterval(() => {
if (sendCount < 50) { // Send for 50 * 100ms = 5 seconds
client.sendAudioData(dummyAudio);
sendCount++;
} else {
clearInterval(interval);
console.log('SimliClient: Finished sending dummy audio data.');
}
}, 100);
});
client.on('disconnected', () => console.log('SimliClient: Disconnected from the server.'));
client.on('failed', (error: Error) => console.error('SimliClient: Connection failed:', error));
try {
console.log('SimliClient: Initializing with provided configuration...');
await client.Initialize(config);
console.log('SimliClient: Initialization complete. Attempting to start connection...');
await client.start(); // This establishes the WebRTC connection
console.log('SimliClient: Connection process initiated.');
// In a real application, you might acquire a MediaStream from getUserMedia
// and then use client.listenToMediastreamTrack(audioTrack);
} catch (error) {
console.error('SimliClient: Fatal error during setup or connection:', error);
client.close(); // Ensure resources are cleaned up on failure
}
}
// Execute the setup function
setupSimliConnection();