The useWebRTC hook handles all the necessary logic to manage a WebRTC connection with the Outspeed’s backend.

Options

config
TRealtimeConfig
  • Required
  • This configuration is used by the useWebRTC hook to initialize local audio and video streams, set up peer connections, enable logging, and more.
  • Learn more about config here.

Returns

connectionStatus
TWebRTCConnectionStatus
  • Init when the component mounts and config is undefined.
  • Connecting when connect is called and the connection to the backend is in progress.
  • Connected when the connection to the backend is successful.
  • Failed if the connection to the backend fails.
  • Disconnected when successfully disconnected from the backend.
connect
() => void
  • A function that initiates the connection to the backend.
disconnect
() => void
  • A function to disconnect from the backend.
dataChannel
WebRTCDataChannel
  • A WebRTC data channel for sending and receiving messages.
  • To send a message to the backend, use dataChannel.send({ type: "message", data: "Hello" }).
  • To listen for messages from the backend, use dataChannel.addEventListener('message', (message) => console.log(message)).
  • To remove a message listener, use dataChannel.removeEventListener('message', onMessage).
getLocalAudioStream
() => MediaStream | null
  • Returns the first local audio stream, or null if no audio track is available.
  • This is a reactive function that may trigger a re-render when a track is added or removed.
getLocalVideoStream
() => MediaStream | null
  • Returns the first local video stream, or null if no video track is available.
  • This is a reactive function that may trigger a re-render when a track is added or removed.
remoteStreams
MediaStream[]
  • An array of remote media stream.

Resources