@fanno/webrtc-client
Version:
A TypeScript-based WebRTC client SDK built on top of LiveKit for real-time video and audio communication
176 lines (134 loc) ⢠4.53 kB
Markdown
A TypeScript-based WebRTC client SDK built on top of LiveKit for real-time video and audio communication.
- š„ Real-time video and audio communication
- š§ TypeScript-first with comprehensive type safety
- š Easy-to-use factory functions
- š Built-in logging and debugging
- š Extensible configuration system
- š Modern ES2020/CommonJS compatible
- š **Dual CJS/ESM builds** - Works in both Node.js and browser environments
- š **Browser-safe** - Proper server-side token generation with client-side security
```bash
npm install
npm run build
```
Create a `.env` file or set environment variables:
```env
LIVEKIT_API_KEY=your_api_key_here
LIVEKIT_API_SECRET=your_api_secret_here
LIVEKIT_URL=ws://localhost:7881 # Optional, defaults to localhost
```
```javascript
const { createRoomClient } = require('./dist/index.js');
async function startVideoCall() {
// Create room client with automatic configuration
const roomClient = createRoomClient();
// Connect to a room
await roomClient.connectToRoom({
roomName: 'my-video-room',
participantIdentity: 'user-123',
participantMetadata: 'John Doe'
});
console.log('Connected to room!');
console.log('Room info:', roomClient.getRoomInfo());
}
startVideoCall().catch(console.error);
```
```javascript
const { createRoomClientWithConfig } = require('./dist/index.js');
const roomClient = createRoomClientWithConfig({
url: 'wss://your-livekit-server.com',
apiKey: 'your-api-key',
apiSecret: 'your-api-secret',
roomOptions: {
adaptiveStream: true,
dynacast: true
}
});
```
The SDK supports both Node.js and browser environments with dual CJS/ESM builds. For browser security, use a token provider instead of API keys.
```javascript
// ES module import in browser
import { createRoomClientWithConfig } from '@fanno/webrtc-client';
// Token provider function that calls your server
async function tokenProvider(params) {
const response = await fetch('/api/token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params)
});
const data = await response.json();
return data.token;
}
// Create browser-safe room client
const roomClient = createRoomClientWithConfig({
url: 'wss://your-livekit-server.com',
tokenProvider: tokenProvider, // Secure token generation
roomOptions: {
adaptiveStream: true,
dynacast: true
}
});
```
The package provides proper module exports for different environments:
```json
{
"main": "dist/index.cjs.js", // CommonJS for Node.js
"module": "dist/index.esm.js", // ES modules for browsers/bundlers
"types": "dist/index.d.ts", // TypeScript declarations
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.esm.js",
"require": "./dist/index.cjs.js"
}
}
}
```
**š See [BROWSER_USAGE.md](./BROWSER_USAGE.md) for complete browser setup guide and security best practices.**
- `createRoomClient()` - Creates a room client with environment configuration
- `createRoomClientWithConfig(config)` - Creates a room client with custom configuration
- `setLogLevel(level)` - Sets the logging level ('debug', 'info', 'warn', 'error')
### RoomClient Methods
- `connectToRoom(params)` - Connect to a LiveKit room
- `disconnectFromRoom()` - Disconnect from the current room
- `getConnectionStatus()` - Get current connection status
- `getRoomInfo()` - Get room information
- `getLocalParticipant()` - Get local participant info
- `getRemoteParticipants()` - Get all remote participants
## Development
### Build the project
```bash
npm run build
```
### Test the SDK
```bash
node test-sdk.js
```
### Project Structure
```
src/
āāā client/ # Room client implementation
āāā types/ # TypeScript type definitions
āāā utils/ # Utilities (config, logging)
āāā index.ts # Main entry point
```
## Environment Variables
| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `LIVEKIT_API_KEY` | ā
| - | LiveKit API key |
| `LIVEKIT_API_SECRET` | ā
| - | LiveKit API secret |
| `LIVEKIT_URL` | ā | `ws://localhost:7881` | LiveKit server URL |
## License
ISC License