Publishing Guide
RtcPublisher publishes local media (camera, microphone, screen, or custom streams) to your WebRTC backend.
This guide focuses on practical publishing workflows, source switching, and production-oriented operational patterns.
Basic Usage
typescript
import { RtcPublisher } from '@webrtc-engine/core';
const publisher = new RtcPublisher({
url: 'webrtc://localhost/live/mystream',
api: 'http://localhost:1985/rtc/v1/publish/',
source: { type: 'camera', audio: true },
target: document.getElementById('preview') as HTMLVideoElement,
});
publisher.on('streamstart', () => console.log('Publishing started'));
publisher.on('sourcechange', (source) => console.log('Source changed:', source.type));
publisher.on('permissiondenied', ({ error }) => {
console.error('Permission denied:', error.message);
});
await publisher.start();Media Source Types
typescript
// Camera (optionally with microphone)
{ type: 'camera', audio: true }
// Microphone (audio-only publishing)
{ type: 'microphone' }
// Screen sharing (optionally with system audio)
{ type: 'screen', audio: true }
// Custom stream
{ type: 'custom', stream: existingMediaStream }Switch Source During Publishing
You can switch capture sources without recreating the publishing session:
typescript
await publisher.switchSource({ type: 'screen', audio: true });Lifecycle Management
typescript
await publisher.start(); // Start publishing
publisher.stop(); // Stop publishing
publisher.destroy(); // Release resources and destroy instanceAccess Current Local Stream
typescript
const stream = publisher.getStream();
if (stream) {
console.log('Video tracks:', stream.getVideoTracks().length);
console.log('Audio tracks:', stream.getAudioTracks().length);
}Best Practices
- Handle permission denial explicitly: Listen to
permissiondeniedand provide clear user guidance - Always clean up lifecycle resources: Call
destroy()when page/component is disposed - Use HTTPS in production: Media capture and WebRTC capabilities require secure contexts
- Invest in observability: Combine
state,error, andsourcechangefor diagnostics
See RtcPublisher API and Plugin System.