RelayrtcDocs
Get started
Documentation
Build calls

TURN configuration

Choose direct or relayed media connectivity and renew temporary TURN credentials.

On this page

TURN relays media when a browser cannot connect directly to the media server, or when your application requires relay-only connectivity. It does not replace signaling, and it does not relay HLS HTTP playback.

When TURN is used

Each browser client has its own ICE policy and credentials. Participants in the same room can use different policies, and a participant's send and receive transports can select different routes.

PolicyConfigurationBehavior
AutomaticiceTransportPolicy: 'all' with TURN availableDirect or relay candidates may be selected.
Require TURNiceTransportPolicy: 'relay' with valid TURN credentialsMedia cannot fall back to a direct connection.
No TURNiceTransportPolicy: 'all', iceServers: [], no TURN credentials/callbackOnly direct media connectivity is available.

Choose the policy before joining. Changing a variable or provider prop alone does not switch an active client's ICE configuration; leave and recreate the configured client/provider before rejoining.

Requirements

Configure a TURN service reachable from participant browsers. Typical deployments offer TURN UDP on port 3478 and TURN TLS on port 5349, but use your operator's actual hostname, ports, transport and relay-port firewall configuration.

localhost refers to the participant's machine, not your server. A successful room join does not verify TURN or media reachability.

Configure RelayClient

Provide iceServers for your TURN service, or temporary credentials through the backend callback below. Do not embed shared TURN authentication secrets in the browser. Static credentials in frontend code are visible to its users.

TypeScript
import { RelayClient } from '@relayrtc/client'
 
export function createRelayOnlyClient(signalingUrl: string, iceServers: RTCIceServer[]) {
  return new RelayClient({ signalingUrl, iceServers, iceTransportPolicy: 'relay', autoSubscribe: true })
}

This helper expects ICE server entries obtained through your authorized configuration flow. Use the same iceServers and iceTransportPolicy options on React's RelayProvider.

Issue temporary credentials

Your backend can issue temporary credentials through relay.turn.createCredentials. Use the quickstart's relay.ts and a key with tokens:create.

TypeScript
import { relay } from './relay.js'
 
export async function issueTurnCredentials(roomId: string, sessionId: string) {
  return relay.turn.createCredentials({ roomId, sessionId })
}

Your application implements /api/turn: authenticate the caller, validate the supplied identifiers, check that the caller owns or may act for the selected room/session, and only then invoke the helper. Return the credential response unchanged; it contains the ICE servers and expiration information the browser needs. Never return the API key or the TURN shared secret.

Use a stable callback when creating the provider:

React
'use client'
 
import { RelayProvider, RelayRoom } from '@relayrtc/react'
import type { CredentialRefreshContext } from '@relayrtc/react'
import type { ReactNode } from 'react'
 
async function refreshTurnCredentials({ roomId, sessionId, signal }: CredentialRefreshContext) {
  const response = await fetch('/api/turn', {
    method: 'POST', signal, headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ roomId, sessionId }),
  })
  if (!response.ok) throw new Error('TURN credentials are unavailable')
  return response.json()
}
 
export function RelayedCall({ token, signalingUrl, children }: {
  token: string; signalingUrl: string; children: ReactNode
}) {
  return <RelayProvider signalingUrl={signalingUrl} iceTransportPolicy="relay"
    refreshTurnCredentials={refreshTurnCredentials} autoSubscribe>
    <RelayRoom token={token}>{children}</RelayRoom>
  </RelayProvider>
}

The callback obtains initial credentials when necessary and renews them before expiry. The default refresh margin is 60 seconds. The SDK updates ICE servers and restarts ICE during renewal. Participant token renewal is a separate configuration; see connection recovery.

Check the connection

Inspect each selected media transport in browser WebRTC diagnostics. In Chromium, open chrome://webrtc-internals before joining. A selected local relay candidate indicates a TURN allocation for that transport. A direct route in automatic mode is allowed; a connected WebSocket alone says nothing about the media route.

Matching newer SDK builds expose room.getMediaConnectionStats() with route, state, protocols and byte counters per local transport. See media connection stats. These diagnostics are distinct from server usage totals.

Troubleshooting

SymptomAction
Room joins but relay-only media failsCheck TURN credentials, host/ports, TLS configuration and relay-port firewall rules.
Local testing works but remote browsers failReplace localhost/container-only addresses with publicly reachable configuration.
Credentials are rejectedCheck expiry and issuer/server authentication configuration.
Long calls lose relay connectivityInspect the authenticated credential callback and its renewal errors.
Direct candidates are still selectedConfirm the client was created with relay-only policy before joining.

See troubleshooting calls for capture, subscription and autoplay failures.