API
useWebSocket
A hook to manage outspeed’s WebSocket connection.
The useWebSocket
hook handles all the necessary logic to manage a web socket connection with the Outspeed’s backend.
Options
- Required
- This configuration is used by the
useWebSocket
hook to initialize local audio streams, set up peer connections, enable logging, and more. - It is an object with the following properties:
functionURL
: The URL of the function to be called for establishing the connection.audio
: Audio constraints that will be passed to thenavigator
.logger
: Logger configuration for managing and storing logs related to the connection. This must implement the Logger class.
Returns
- Possible values:
new
when the component or hook mounts.connecting
whenconnect
is called and the connection to the backend is in progress.connected
when the socket connection to the backend is successful.failed
if the socket connection to the backend fails.disconnected
when the socket connection is disconnected from the backend.
- A function that initiates the connection to the backend.
- A function to disconnect from the backend.
- 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).
- 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 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.
Resources
Was this page helpful?