Documentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/partykit/llms.txt
Use this file to discover all available pages before exploring further.
partytracks is a client/server library for real-time audio and video in web applications. It wraps Cloudflare Realtime SFU with an Observable-based API that handles WebRTC complexity — peer connection recovery, hardware changes, network switches, device selection, and track lifecycle — so your application code does not need to.
Installation
Class: PartyTracks
Import frompartytracks/client. PartyTracks handles all WebRTC negotiation through push and pull methods backed by Observables.
Constructor
Optional configuration. See PartyTracksConfig below.
Methods
push(sourceTrack$, options?)
Push a local track to the Realtime SFU. Returns an Observable of TrackMetadata that you share with other peers so they can pull the track.
If sourceTrack$ emits a new track, it replaces the old one on the transceiver. If the peer connection is disrupted, the track is automatically re-pushed and new TrackMetadata is emitted.
An Observable emitting the local
MediaStreamTrack to broadcast. Use
getMic().broadcastTrack$ or getCamera().broadcastTrack$ here.Optional Observable of RTP encoding parameters (e.g. for simulcast). Initial
values are applied on push; subsequent emissions update the encodings.
Observable<TrackMetadata> — send the emitted metadata to other peers so they can call pull.
pull(trackData$, options?)
Pull a remote track from the Realtime SFU. Returns an Observable of MediaStreamTrack.
If trackData$ emits new metadata (e.g. because the remote peer re-pushed) or if the peer connection is disrupted, the track is automatically re-pulled.
An Observable of
TrackMetadata received from the remote peer.For simulcast tracks, an Observable that emits the preferred RID (layer
identifier) to receive.
Observable<MediaStreamTrack> — attach to a MediaStream or pass to createAudioSink.
Properties
Emits the active
RTCPeerConnection. When the connection is disrupted, a new
one is created and emitted automatically.Emits the active peer connection together with its associated session ID.
Flows from
peerConnection$; emits a new pair when the connection changes.Emits each
RTCRtpTransceiver as it is added to the peer connection.Emits the current
RTCPeerConnection.connectionState value whenever it changes.A rolling log of API calls made by
PartyTracks. Useful for debugging. Capped
at maxApiHistory entries (default 100).PartyTracksConfig
Optional configuration object for thePartyTracks constructor.
Additional query parameters appended to every API request, e.g.
"userId=123&roomId=456".Custom ICE servers. If omitted, ICE servers are fetched from the
/partytracks/generate-ice-servers endpoint on your proxy.The pathname prefix your proxy uses, e.g.
"/api/partytracks". Provide a
full URL (e.g. "https://api.example.com/partytracks") for cross-domain
connections.Maximum number of
ApiHistoryEntry records to retain in history.Custom headers appended to every API request (e.g. for authentication).
getMic(options?)
Returns aMediaDevice representing the user’s microphone. By default the mic source is kept alive (retainIdleTrack: true) for “talking while muted” detection.
getCamera(options?)
Returns aMediaDevice representing the user’s camera.
MediaDeviceOptions
BothgetMic and getCamera accept the same options:
Whether the track should start broadcasting immediately.
Keep the source track active even when there are no subscribers. Defaults to
true for mic, false for camera.Initial set of track transformations (e.g. noise suppression, blur).
Whether
isSourceEnabled should be true from the start.Constraints passed to
navigator.mediaDevices.getUserMedia(). deviceId and
groupId are excluded because partytracks tries all available devices
automatically when the preferred device is unavailable.Callback invoked when an individual device fails to produce a healthy track.
Use this to surface warnings to the user or deprioritize the device.
MediaDevice interface
BothgetMic and getCamera return a MediaDevice:
Emits the browser permission state for this device (
"granted", "denied", "prompt").Emits the list of available devices of this kind. Use to populate a device-selection UI.
Emits the currently active device, the preferred device (if set), or the default device.
Sets and persists (via
localStorage) the user’s preferred device. If the
preferred device is unavailable, all other devices are tried. If it becomes
available again, it is automatically selected.The track to pass to
partyTracks.push(). Switches to a silent/empty fallback
track when broadcasting is stopped.An always-on monitor track. Primarily useful for the mic to enable
“talking while muted” detection. Avoid for cameras unless users clearly
understand the camera light will stay on.
Whether content is actively being sent.
Whether the content source is enabled. Flips to
false on errors or when the
source ends (e.g. screenshare stopped).Emits errors encountered acquiring the source — most commonly
NotAllowedError
or DevicesExhaustedError.getScreenshare(options?)
Returns aScreenshare object whose audio and video properties each expose broadcast/transform APIs. Source enabled state is shared at the top level.
ScreenshareOptions
Whether
isSourceEnabled should be true initially.Keep source tracks alive even when there are no subscribers.
Enable audio capture, optionally with constraints and broadcast defaults.
Enable video capture, optionally with constraints and broadcast defaults.
createAudioSink
UsecreateAudioSink to safely play pulled audio tracks. It handles edge cases that prevent audio from playing correctly when tracks are attached directly to an HTMLAudioElement.
The
<audio> element to play audio through.audioSink.attach(pulledTrack$) returns a Subscription. Call .unsubscribe() to stop playback and clean up.
routePartyTracksRequest (server)
Import frompartytracks/server. Proxies all requests to the Cloudflare Realtime SFU API, injecting your app credentials. Mount it on a wildcard path in your Worker.
Your Cloudflare Realtime SFU application ID.
Your Cloudflare Realtime SFU application token.
The incoming
Request object to proxy.Optional TURN server application ID. When provided alongside
turnServerAppToken, the /partytracks/generate-ice-servers endpoint returns
TURN credentials in addition to STUN servers.Optional TURN server application token.
Lifetime in seconds for generated TURN credentials.
Server setup with Hono
React Utilities
Import frompartytracks/react. By convention, Observable variables carry a $ suffix.
Creates a stable Observable that emits whenever
value changes between renders.Subscribes to
observable$ and returns the latest emitted value. Returns
defaultValue until the first emission.Subscribes to
observable$ and calls observer.next, observer.error, and/or
observer.complete as events arrive.Install
webrtc-adapter and import it before any partytracks/client code to
smooth out cross-browser WebRTC behavior differences:
import "webrtc-adapter";