Skip to content

Events

WebRTC Engine uses an event-driven architecture. You can observe connection transitions, media updates, and runtime failures, then map them into robust business workflows.

Quick Classification

  • Common events: emitted by both RtcPlayer and RtcPublisher
  • Playback events: emitted only by RtcPlayer
  • Publishing events: emitted only by RtcPublisher

1) Common Events (Player / Publisher)

These events are available on both RtcPlayer and RtcPublisher:

Full state values: connecting, connected, disconnected, failed, closed, switching, switched, destroyed (see RtcState)

EventDescriptionPayload type
stateRTC state changed (connecting/connected/...)RtcState
errorRuntime error occurredstring
icecandidateLocal ICE candidate generatedRTCIceCandidate
iceconnectionstateICE connection state updatedRTCIceConnectionState
icegatheringstateICE gathering state updatedRTCIceGatheringState
reconnectingAuto-reconnect attempt started{ retryCount, maxRetries, interval }
reconnectfailedAuto-reconnect exhausted (max retries reached){ maxRetries }
reconnectedAuto-reconnect succeededvoid
signalingerrorSignaling request failed{ error, request? }
typescript
const onState = (state: RtcState) => {
  console.log('State changed:', state);
};

const onError = (error: string) => {
  console.error('Error:', error);
};

instance.on('state', onState);
instance.on('error', onError);

2) Playback Events (only RtcPlayer)

EventDescriptionPayload type
trackRemote media stream arrived{ stream, event }
mediareadyMedia element entered playable state{ stream }
typescript
const onRemoteTrack = (payload: { stream: MediaStream; event: RTCTrackEvent }) => {
  videoEl.srcObject = payload.stream;
};

player.on('track', onRemoteTrack);

3) Publishing Events (only RtcPublisher)

EventDescriptionPayload type
streamstartPublishing has started{ stream }
streamstopPublishing has stoppedvoid
streamingstatechangePublishing state changed'idle' | 'connecting' | 'streaming'
sourcechangeCapture source switchedMediaSource
permissiondeniedMedia permission was denied{ source, error: Error }
trackRemote media stream arrived (echo){ stream, event }
trackendedLocal track ended{ track, stream, reason: 'ended' }
trackmutechangedLocal track mute/unmute state changed{ track, muted: boolean }
typescript
publisher.on('streamstart', ({ stream }) => {
  console.log('Publishing started', stream);
});

publisher.on('permissiondenied', ({ source, error }) => {
  console.warn('Permission denied:', source, error);
});

Listener patterns

typescript
const onTrack = (payload: { stream: MediaStream }) => {
  console.log('Remote stream received', payload.stream);
};

player.on('track', onTrack);

// Listen once
player.once('error', (error) => {
  console.error('Error:', error);
});

// Remove listener
player.off('track', onTrack);

Best practices

  • Design listeners by layers first (common / playback / publishing) to avoid mixed responsibilities
  • Map state into your app state machine (connecting, connected, reconnecting, failed)
  • Standardize error classification and reporting
  • For permissiondenied, provide actionable guidance (browser permissions, retry path)

See RtcState.

基于 MIT 许可证发布