RelayrtcDocs
Get started
Documentation
Build calls

Screen sharing

Publish a display, render its preview and receive screen video and audio.

On this page

Screen sharing publishes a display chosen in the browser's picker. The token needs screen:publish. Call screen.start directly from a user interaction in a connected room; the browser controls which surfaces and audio options are available.

Requirements

Use HTTPS or localhost and render the component inside RelayProvider and RelayRoom. Do not enable sharing automatically on page load. Requesting audio: true does not guarantee that the chosen tab, window or display supplies audio.

Start sharing

This component handles the picker, local preview and stopping the share:

React
'use client'
 
import { useState } from 'react'
import { MediaLoader, useLocalMedia, useLocalParticipant, useMediaTrack, useRoom } from '@relayrtc/react'
 
export function ScreenShare() {
  const local = useLocalParticipant()
  const { connectionState } = useRoom()
  const { screen } = useLocalMedia()
  const preview = useMediaTrack(screen?.videoTrack, { paused: screen?.muted ?? false })
  const [error, setError] = useState<string | null>(null)
 
  async function toggle() {
    if (!local) return
    setError(null)
    try {
      if (screen?.videoTrack) await local.screen.stop()
      else await local.screen.start({ video: true, audio: true })
    } catch (cause) {
      setError(cause instanceof Error ? cause.message : 'Unable to share this screen')
    }
  }
 
  return (
    <div>
      <button disabled={!local || screen?.pending || connectionState !== 'connected'}
        onClick={() => void toggle()}>{screen?.videoTrack ? 'Stop sharing' : 'Share screen'}</button>
      <video ref={preview.ref} autoPlay muted playsInline
        hidden={!screen?.videoTrack} style={{ width: '100%', objectFit: 'contain' }} />
      {(screen?.pending || preview.isLoading) && <MediaLoader label="Starting screen share" />}
      {error && <p role="alert">{error}</p>}
    </div>
  )
}

Picker cancellation or denied permission rejects the start promise. Capture remains inactive. If the application stops or leaves while the picker is open, tracks returned later are discarded and stopped.

Show loading and local preview

The component above uses the screen's local video track. Keep its preview muted. Use object-fit: contain to avoid cropping the shared content, and reserve space for the preview while media is loading.

Receive screens

Remote screen video has type screen_video; display audio has type screen_audio. They are separate from camera video and microphone audio. Add these track types to the quickstart's renderer:

React
import { useTracks } from '@relayrtc/react'
 
export function useScreenTracks() {
  return useTracks({ types: ['screen_video', 'screen_audio'] })
}

Render each returned track with useMediaTrack, using a video element for screen video and an audio element for screen audio. The quickstart's RemoteMedia component already distinguishes these element types. Supply screen tracks to that component and keep its blocked-autoplay button for sound.

If the browser does not supply display audio, no screen-audio publication appears. If display audio ends independently, the SDK removes that audio publication while keeping screen video active.

Stop sharing

await local.screen.stop() releases the capture and removes its publications. The SDK also handles the browser's Stop sharing control. Use mute()/unmute() to pause and resume publications while keeping capture open; await their server confirmation.

See screen controls for supported options and snapshots.

Live room permissions

Room hosts and moderators can restrict screen:publish while a participant is connected. Revocation applies to both screen video and display audio. Use the room policy guide for effective capability state, server enforcement timing and host delegation. Browser capture permission remains a separate requirement.