Browser client
Use the room API without React.
On this page
Install
pnpm add @relayrtc/clientSee installation for package requirements.
Requirements
A browser with WebRTC, a signaling URL, and a participant token issued by your backend. Media capture needs HTTPS or localhost and the appropriate participant permissions.
Create a client
import { RelayClient } from '@relayrtc/client'
const client = new RelayClient({
signalingUrl: 'ws://localhost:8081/v1/connect',
autoSubscribe: true,
})The configuration is the same browser configuration accepted by RelayProvider, excluding React props, messageHistoryLimit and onError.
Connection mode per participant
Each RelayClient has its own ICE configuration. Participants in the same room can choose different modes: iceTransportPolicy: 'all' allows Automatic selection, while iceTransportPolicy: 'relay' requires TURN for that client's media connection.
Provide TURN servers or temporary credentials when you want TURN to be available. To disable it, use 'all' with iceServers: [] and no turnCredentials or refreshTurnCredentials. Automatic alone does not mean TURN is disabled.
Choose the policy when creating the client. Leave the current call and create a client with the new settings to change modes. See TURN configuration for the mode comparison and connection checks.
The browser example shows a client factory accepting Automatic, Force TURN or TURN disabled for each participant, with temporary credential renewal.
Join and publish
const token = await fetch('/api/join', { method: 'POST' })
.then((response) => {
if (!response.ok) throw new Error('Unable to join')
return response.json() as Promise<{ token: string }>
})
const room = await client.join(token.token)
await room.camera.enable()
await room.microphone.enable()Your application implements the authenticated /api/join endpoint. Joining alone does not open devices.
Pause and resume publications
await room.microphone.mute()
await room.microphone.unmute()
await room.camera.mute()
await room.camera.unmute()Mute/unmute return promises that confirm the server publication state. Catch failures in your controls. Capture remains open, and remote tracks receive paused/resumed state updates. Use disable() to stop capture and remove a microphone or camera publication; use screen.stop() for screen sharing. Starting a screen requires a user gesture and screen:publish permission. See capture controls and screen sharing.
Subscribe to events
const stop = client.on('trackPublished', (track) => {
if (track.type !== 'camera_video') return
const video = document.createElement('video')
video.autoplay = true
video.playsInline = true
document.body.append(video)
void track.attach(video).catch(console.error)
})Call the returned cleanup function when removing the listener. Handle rejected playback when the browser requires a user gesture.
stop()Methods and properties
| Member | Purpose |
|---|---|
| currentRoom | Current Room, or undefined when there is no room. |
| connectionState | connecting, connected, reconnecting, disconnected or failed. |
| join(token, options?) | Promise of Room. Optional signal cancels joining. |
| leave() | Promise of void. Leaves this client’s current room. |
| on(event, listener) | Register a RoomEvents listener and return its cleanup function. |
Only one room can be active on a client. Leave it before joining another room.
The returned Room provides camera, microphone, screen sharing, messages, metadata, remote tracks, credentials and quality statistics. Those methods work the same way outside React.
Inspect media connections
The joined Room exposes getMediaConnectionStats(). It returns a local snapshot of the send and receive transports, including their state, actual route, protocols and selected-pair byte counters. This is separate from the requested connection policy and the room's signaling state.
import type { MediaConnectionStats } from '@relayrtc/client'
const snapshot: MediaConnectionStats = await room.getMediaConnectionStats()
for (const transport of snapshot.transports) {
console.log(transport.direction, transport.state, transport.route)
console.log(transport.protocol, transport.relayProtocol)
console.log(transport.bytesSent, transport.bytesReceived)
}Here, room is the result of client.join(). Handle rejected requests when the room closes or browser statistics are unavailable. Use a matching browser SDK build that includes this method and its types.
The values describe this client's selected ICE pairs, not project usage or invoice totals. Counters can change or reset when a pair or transport changes. No selected pair produces an unknown route and zero fallback counters. See the shared Room reference and stats types.
Leave
await client.leave()Leaving releases the local session and media. To end the call for every participant, use rooms.end on your backend.
Room access policies
room.access exposes effective capabilities and versioned participant policy reads/updates. capabilitiesChanged observes local authorization changes. See room roles and live permissions.