The useWebSocket hook handles all the necessary logic to manage a web socket connection with the Outspeed’s backend.

Options

config
TRealtimeWebSocketConfig
  • 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 the navigator.
    • logger: Logger configuration for managing and storing logs related to the connection. This must implement the Logger class.

Returns

connectionStatus
TWebSocketConnectionStatus
  • Possible values:
    • new when the component or hook mounts.
    • connecting when connect 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.
connect
() => void
  • A function that initiates the connection to the backend.
disconnect
() => void
  • A function to disconnect from the backend.
dataChannel
WebsocketDataChannel
  • 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), and message (triggered when a message is received).
getLocalAudioTrack
() => Track | null
  • 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.
getRemoteAudioTrack
(remoteId: string) => Track | null | undefined
  • Returns the first active remote audio track, or null if it doesn’t exist. If the connection is still in progress, it may return undefined. 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