RelayrtcDocs
Get started
Documentation
React SDK

RelayProvider

Own the room client and make reactive state available to child hooks.

On this page

Import

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

Requirements

Use one provider for each independent room client. Put all room hooks inside it. In Next.js, render the call from a client module.

Props

PropTypeDefaultPurpose
signalingUrlstringRequiredws:// or wss:// URL. No credentials, query or fragment.
childrenReactNodeOptionalYour room and UI.
autoSubscribebooleantrueSubscribe to remote media automatically.
videoSimulcastbooleantruePublish video using multiple encodings. Set false before joining for one full-resolution video encoding.
requestTimeoutMsnumber10,000Signaling request timeout, 1–60,000 ms.
iceServersreadonly RTCIceServer[]OptionalAdditional/custom ICE configuration.
iceTransportPolicy'all' or 'relay'allAllow all candidates or require relay.
turnCredentialsTurnCredentialsOptionalInitial TURN response with expiresAt and iceServers.
refreshTurnCredentialsasync callbackOptionalFetch server-issued TURN credentials for the active session.
refreshTokenasync callbackOptionalReturn an identity-preserving replacement token. See expiry limits below.
credentialRefreshMarginMsnumber60,000Refresh margin, 1,000–300,000 ms.
reconnectfalse or objectEnabledSee reconnect options below.
messageHistoryLimitnumber100Integer 0–10,000. Zero disables local history.
onError(error: RoomError) => voidOptionalObserve SDK failures.

Connection mode per participant

ICE settings apply to this provider's client connection. They do not set a policy for the whole room. One participant can use iceTransportPolicy="all" for Automatic while another participant in the same room uses iceTransportPolicy="relay" for Force TURN.

Automatic can select a direct or relayed route when TURN credentials are supplied. Force TURN requires valid credentials and a reachable TURN server. To disable TURN, use iceTransportPolicy="all", pass iceServers={[]}, and omit turnCredentials and refreshTurnCredentials.

Choose these props before joining. Leave and rejoin with the new configuration to change modes. See TURN configuration for mode selection and route verification.

The React example shows a call component accepting a mode for the current participant and supplying temporary credentials only when TURN is enabled.

Video encodings

videoSimulcast applies to camera and screen video publications on this client. It changes sender encodings, not capture resolution or the stream's HLS output profile. Set it before joining; leave and rejoin to apply a different connection configuration. A matching browser SDK is required.

Reconnect options

OptionDefaultLimits
maxAttempts6Integer 1–20.
timeoutMs25,000Overall deadline, 1,000–120,000 ms.
initialDelayMs250Nonnegative; no greater than maxDelayMs.
maxDelayMs4,0001–30,000 ms.

Example

React
import type { ReactNode } from 'react'
 
export function CallProvider({ children, signalingUrl }: {
  children: ReactNode; signalingUrl: string
}) {
  return <RelayProvider
    signalingUrl={signalingUrl}
    autoSubscribe
    messageHistoryLimit={200}
    reconnect={{ maxAttempts: 6, timeoutMs: 25000 }}
    onError={error => console.error(error.code, error.message)}
  >{children}</RelayProvider>
}

Pass a child component rendering RelayRoom or calling useRoom().join().

Credential callbacks

Callbacks receive roomId, participantId, sessionId, expiresAt, reason and an AbortSignal. Reason is initial, expiring or manual.

Return credentials from an authenticated backend. Do not expose the management key.

The creation API creates a new participant. For an ongoing call, connect refreshToken to an authorized backend using tokens.renew. This preserves identity/session and permissions. Temporary retryable failures retry before expiry; denial stops renewal. See connection recovery. See installation for compatible package builds.

Lifecycle

Unmounting the provider disposes its store and leaves the room. Treat connection options as configuration for the active client; leave and join again to apply different connection settings.