API
useWebRTC
A hook to manage outspeed’s WebRTC connection.
The useWebRTC
hook handles all the necessary logic to manage a WebRTC connection with the Outspeed’s backend.
Options
- 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
Init
when the component mounts andconfig
is undefined.SetupCompleted
whenconfig
is defined butconnect
hasn’t been called.Connecting
whenconnect
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.Disconnecting
when attempting to disconnect from the backend.Disconnected
when successfully disconnected from the backend.
- A function that initiates the connection to the backend.
- Be sure to call this when
connectionStatus
isSetupCompleted
, otherwise it will have no effect.
- A function to disconnect from the backend.
- Be sure to call this when
connectionStatus
isConnected
, otherwise it will have no effect.
- A function to reset the connection.
- Be sure to call this when
connectionStatus
isDisconnected
, otherwise it will have no effect. - This will set the
connectionStatus
toInit
.
- 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)
. - The available events are
open
(triggered when the data channel opens),close
(triggered when the data channel closes), andmessage
(triggered when a message is received).
addEventListener
(type: TRealtimeConnectionListenerType, listener: TRealtimeConnectionListener) => TUseWebRTCReturn
- To attach an event listener to the RTCPeerConnection.
- Refer to this MDN doc to learn more.
removeEventListener
(type: TRealtimeConnectionListenerType, listener: TRealtimeConnectionListener) => TUseWebRTCReturn
- To remove an event listener from the RTCPeerConnection.
- Returns the first local audio track, 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.
- Returns the first local video track, 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.
- Returns the first active remote audio track, or
null
if it doesn’t exist. If the connection is still in progress, it may returnundefined
. Once the connection is updated, it will return the track. - This is a reactive function that may trigger a re-render when a track is added or removed.
- Returns the first active remote video track, or
null
if it doesn’t exist. If the connection is still in progress, it may returnundefined
. Once the connection is updated, it will return the track. - This is a reactive function that may trigger a re-render when a track is added or removed.
Resources
Was this page helpful?