Installation
Basic Setup
For React applications, you’ll typically create a component that manages the WebRTC connection and video streams.Video Stream Component
Here’s a complete example of a React component that connects to the Decart real-time API:Using the Component
Key Patterns
1. Using Refs for Video Elements
UseuseRef to get direct access to video elements for setting srcObject:
2. Storing the Client in a Ref
Store the realtime client in a ref to access it outside of the setup effect:3. Cleanup on Unmount
Always disconnect the client when the component unmounts:4. State Management
Use React state to track connection status and errors:5. Updating Model State
Use a separate effect to update the model state when props change:Connection States
The SDK emits the following connection states:connecting- Initial connection in progressconnected- WebRTC connection establishedgenerating- Model is generating framesreconnecting- Attempting to reconnect after disconnectiondisconnected- Connection closed
Best Practices
- Always cleanup: Disconnect the client in the cleanup function to prevent memory leaks
- Check if mounted: Use a
mountedflag to avoid state updates on unmounted components - Check connection status: Before calling methods like
setPrompt(), check if the client is connected - Use the model object: Get video constraints from the model definition for optimal quality
- Handle errors: Subscribe to the
errorevent to handle WebRTC errors gracefully
API Key Management
Development (Direct API Key)
Production (Token from Backend)
For production, fetch a client token from your backend:Next Steps
- Next.js Integration - Learn how to use the SDK with Next.js App Router
- Error Handling - Handle errors and connection issues
- Real-Time API Reference - Detailed API documentation