Getting Started
Prerequisites
Before you begin, make sure you have:- A Simli account with an API key
- Node.js and npm installed in your development environment
Installation
Install thesimli-client package in your project:
Usage
Step 1: Get the Video and Audio element references
If you’re using React, create video and audio elements with refs:Step 2: Create a session token
You can create the session_token from the client side, or put it behind a backend (or use server actions for Next.JS or equivalent in other frameworks). The recommended method for production deployments is to put it behind a backend as you don’t want your api key to reside on the clientIf you’re using listenToMediastreamTrack() set
handleSilence: false in the SimliConfig to avoid audio artifacts(Optional) Step 2.5: Generate Ice Server Config (For running Simli in peer to peer mode)
This provides a slightly lower latency connection to Simli however it might have some issues in places with stronger firewall restrictions (in such case we recommend using livekit mode which doesn’t need this step at all)Step 3: Initialize SimliClient
Create an instance of SimliClient and initialize it with your configuration: For P2P Mode:Step 4: Send Audio Data
Once the connection is established, you can start sending audio data:SimliClient API reference
Types
Methods
async generateSimliSessionToken( request: TokenRequestData, SimliURL: string = "https://api.simli.ai",): Generate new session token given aTokenRequestDataobject. You can also redirect that request to your own backend.new SimliClient(session_token: string, videoElement: HTMLVideoElement, audioElement: HTMLAudioElement, iceServers: RTCIceServer[] | null, logLevel: LogLevel = LogLevel.DEBUG, transport_mode: TransportMode = "p2p", signaling: SignalingMode = "websockets", SimliWSURL: string = "wss://api.simli.ai", audioBufferSize: number = 3000,): Initializes the SimliClient with the provided configuration.async SimliClientInstance.start(): Connects everything to Simli Servers with automatic retry on failureasync SimliClientInstance.stop(): Closes the WebRTC connection and cleans up resources.SimliClientInstance.sendAudioData(audioData: Uint8Array): Sends audio data to the server.SimliClientInstance.listenToMediastreamTrack(stream: MediaStreamTrack): Listens to a MediaStreamTrack and sends audio data to the server. Can be used as an alternative tosendAudioData.SimliClientInstance.ClearBuffer(): Clears the audio buffer, best used when you want the avatar to stop talking.
Advanced Usage
Events
startwhen the webrtc connection is successful and avatar is shown on screenstopwhen the simli server terminates a connection normallyerrorwhen the webRTC connection fails to connect or crashes in the middlespeakingwhen the avatar starts speakingsilentwhen the avatar stops speakingackwhen server acknowledges an audio segment has been receivedconnection_info(serialized_info: string)when rtc answer or livekit session info is received not useful in most applications and is meant for primarily internal usevideo_info(serialized_info: string)shows video FPS, height, and widthdestination(serialized_info: string)debugging information useful when reporting issues to simliunknown(message: string)when the server sent a message that couldn’t be parsed. Usually future messages that have been added to later versions of the clientstartup_error(message: string)when there was a fundamental error causing simli to be unable to connect. This is usually triggered for invalid FaceIDs or depleted minutes. Message should be the cause of this startup failure. Automatic retries are disabled on startup_error
Error Handling
SimliClient provides console logging for various events and errors. It’s recommended to implement proper error handling in your application to manage potential issues, such as network disconnections or initialization failures.Fork and Contribute to simli-client
simli-client
Fork and contribute to the simli-client repository on GitHub or clone for your own usecase.
Troubleshooting
If you encounter issues:- Ensure your API key is correct and active.
- Verify that your face ID is valid and associated with your account.
- Check that your audio data is in the correct format (PCM16, 16KHz).
- Verify that you have the necessary permissions for accessing the user’s media devices.
- Review the console logs for any error messages or warnings.