RelayrtcDocs
Get started
Documentation
React SDK

Room object

Use the underlying room for events, metadata, media and cleanup.

On this page

Access

React
import { useRoom } from '@relayrtc/react'
 
const { room } = useRoom()

Properties

PropertyPurpose
id, infoRoom ID and server room object.
localParticipantLocal identity and controls.
sessionCurrent participant session.
connectionStateConnection lifecycle.
participantsReadonlyMap of remote participants.
remoteTracksReadonlyMap of remote publications.
microphone, camera, screenLocal capture controls.
devices, permissionsDevice enumeration and permission APIs.
messagessend(text) and subscribe(listener).
eventsemit(name, data) and subscribe(listener) for custom events.
presenceLocal and remote participant snapshot.
quality, qualityStats, participantQualitiesNetwork health and statistics.

Methods

MethodPurpose
on(event, listener)Subscribe to a room event; returns cleanup.
refreshCredentials()Run configured credential refresh callbacks. Returns Promise of void.
getMediaConnectionStats()Read local media transport states, routes, protocols and byte counters. Returns Promise of MediaConnectionStats.
leave()Leave this participant’s call. Returns Promise of void.

Example

React
import { useEffect } from 'react'
 
export function ParticipantNotifications() {
  const { room } = useRoom()
  useEffect(() => {
    if (!room) return
    return room.on('participantJoined', participant => console.log(participant.name))
  }, [room])
  return null
}

Import useEffect from React and clean up listeners when the room changes.

Media connection stats

Call room.getMediaConnectionStats() while the room is active to inspect the actual selected route for each send/receive transport. The method is implemented by the browser SDK and is also available through React's useRoom().room. Use matching SDK builds that include the method and the MediaConnectionStats and MediaTransportStats types.

React
import { useState } from 'react'
import type { MediaConnectionStats } from '@relayrtc/react'
 
function MediaConnectionDetails() {
  const { room } = useRoom()
  const [snapshot, setSnapshot] = useState<MediaConnectionStats | null>(null)
  const [error, setError] = useState('')
 
  async function refresh() {
    if (!room) return
    try {
      setError('')
      setSnapshot(await room.getMediaConnectionStats())
    } catch {
      setSnapshot(null)
      setError('Media connection details are unavailable.')
    }
  }
 
  return (
    <section>
      <button disabled={!room} onClick={() => void refresh()}>Refresh media details</button>
      {error && <p role="alert">{error}</p>}
      {room && snapshot?.transports.map((transport) => (
        <p key={transport.direction}>
          {transport.direction}: {transport.state} · {transport.route}
          {' · '}{transport.bytesSent} B sent / {transport.bytesReceived} B received
        </p>
      ))}
    </section>
  )
}

Render this component inside your RelayProvider and active RelayRoom. The example reads on demand; the SDK does not automatically poll this method.

FieldMeaning
observedAtISO timestamp when the SDK completed the snapshot.
directionsend or receive, relative to this browser.
stateBrowser peer connection state, including new, connecting, connected, disconnected, failed or closed. An unused transport can remain new.
routedirect, relay or unknown. A connected transport can still be unknown if candidate details are unavailable.
protocolSelected local candidate protocol, such as udp or tcp, or null.
relayProtocolBrowser-to-TURN protocol when available, or null.
bytesSent, bytesReceivedSelected ICE pair counters in bytes. Zero is returned when no selected pair is available.

Send and receive routes can differ in Automatic mode. Relay detection also recognizes TURN protocol metadata on peer-reflexive candidates; a prflx candidate type alone does not rule out TURN.

Snapshots do not expose candidate addresses or credentials. These browser counters have different scope and update times from server usage; do not use them as billing totals. Counters may reset when the selected pair or transport changes. Requests can reject after leaving or when transport statistics are unavailable.

Events

EventsPayload
connectionStateChangedRoomConnectionState.
errorRoomError.
participantJoined, participantLeft, participantUpdatedRoomRemoteParticipant.
participantReconnectedparticipant and session.
localParticipantUpdatedRoomLocalParticipant.
presenceChangedRoomPresenceSnapshot.
trackPublished, trackUnpublished, trackUpdated, trackSubscribed, trackUnsubscribedRoomRemoteTrack.
trackSubscriptionFailedtrack and error.
messageReceived, messageSentRoomTextMessage.
customEventReceived, customEventSentRoomCustomEvent.
connectionQualityChanged, connectionDegraded, connectionRecoveredConnectionQualityEvent.
qualityStatsUpdatedRtcQualityStats.
reconnectAttemptattempt, delayMs and error.
reconnectedattempts and session.
credentialsRefreshedtokenExpiresAt and turnExpiresAt.

Leave versus end

Leave affects only this participant. Ending a room for everyone is a privileged REST/Node operation.

refreshCredentials() requires at least one configured refresh callback. Calling it does not create a token renewal API.