RelayProvider
Own the room client and make reactive state available to child hooks.
On this page
Import
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
| Prop | Type | Default | Purpose |
|---|---|---|---|
| signalingUrl | string | Required | ws:// or wss:// URL. No credentials, query or fragment. |
| children | ReactNode | Optional | Your room and UI. |
| autoSubscribe | boolean | true | Subscribe to remote media automatically. |
| videoSimulcast | boolean | true | Publish video using multiple encodings. Set false before joining for one full-resolution video encoding. |
| requestTimeoutMs | number | 10,000 | Signaling request timeout, 1–60,000 ms. |
| iceServers | readonly RTCIceServer[] | Optional | Additional/custom ICE configuration. |
| iceTransportPolicy | 'all' or 'relay' | all | Allow all candidates or require relay. |
| turnCredentials | TurnCredentials | Optional | Initial TURN response with expiresAt and iceServers. |
| refreshTurnCredentials | async callback | Optional | Fetch server-issued TURN credentials for the active session. |
| refreshToken | async callback | Optional | Return an identity-preserving replacement token. See expiry limits below. |
| credentialRefreshMarginMs | number | 60,000 | Refresh margin, 1,000–300,000 ms. |
| reconnect | false or object | Enabled | See reconnect options below. |
| messageHistoryLimit | number | 100 | Integer 0–10,000. Zero disables local history. |
| onError | (error: RoomError) => void | Optional | Observe 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
| Option | Default | Limits |
|---|---|---|
| maxAttempts | 6 | Integer 1–20. |
| timeoutMs | 25,000 | Overall deadline, 1,000–120,000 ms. |
| initialDelayMs | 250 | Nonnegative; no greater than maxDelayMs. |
| maxDelayMs | 4,000 | 1–30,000 ms. |
Example
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.