Docs
React SDK

useRoom

Access the active room, connection state and join/leave actions.

Import

React
import { useRoom } from '@relayrtc/react'

Requirements

Call this hook inside RelayProvider, in a React function component or custom hook. A joined room is needed for live data and actions.

Returns

FieldTypeMeaning
roomRoom or nullActive room once the join has completed.
clientRelayClient or nullUnderlying browser client.
connectionStateRoomConnectionStateconnecting, connected, reconnecting, disconnected or failed.
errorRoomError or nullLatest store error.
join(token, options?) => Promise<Room>Join manually. JoinOptions accepts signal.
leave() => Promise<void>Leave the current room.
clearError() => voidClear the displayed store error.

Example

React
const { connectionState, error, leave, clearError } = useRoom()
 
return (
  <div>
    <p>{connectionState}</p>
    {error && <button onClick={clearError}>{error.message}</button>}
    <button onClick={() => void leave().catch(console.error)}>Leave</button>
  </div>
)

Options and behavior

Use either RelayRoom’s automatic join or a manual join flow. Do not start both for the same provider. Null room state during setup is expected.