Live streaming in Moqtail enables real-time delivery of continuously generated content like video streams, audio feeds, or sensor data. The LiveTrackSource class wraps a ReadableStream<MoqtObject> and handles the complexities of live object distribution.
Live tracks are optimized for forward-only delivery. Subscribers can join at any time and receive objects from the current position forward.
The underlying ReadableStream<MoqtObject> that produces objects:
const stream = liveSource.stream
2
Latest Position
Tracks the highest location seen so far:
// Returns undefined until first object arrivesconst latest = liveSource.largestLocationif (latest) { console.log(`Latest: Group ${latest.group}, Object ${latest.object}`)}
3
Event Listeners
Register callbacks for new objects or stream completion:
// Listen for each new objectconst unsubscribe = liveSource.onNewObject((obj) => { console.log(`New object: ${obj.groupId}:${obj.objectId}`)})// Listen for stream endliveSource.onDone(() => { console.log('Live stream ended')})// Clean up when doneunsubscribe()
For ultra-low latency scenarios, use datagram delivery:
import { ObjectForwardingPreference } from 'moqtail-ts'const liveTrack: Track = { fullTrackName: FullTrackName.tryNew('sensors/temperature', 'data'), forwardingPreference: ObjectForwardingPreference.Datagram, trackSource: { live: new LiveTrackSource(sensorStream) }, publisherPriority: 0}// Enable datagrams on the clientconst client = await MOQtailClient.new({ url: 'https://relay.example.com', supportedVersions: [0xff00000b], enableDatagrams: true // Required for datagram delivery})
Datagrams may be lost or delivered out of order. Only use for content where latency is more critical than reliability (e.g., sensor data, audio, low-latency video).
The library implements intelligent stream cancellation:
// Automatically cancels streams for older groups// when bandwidth is limited// The publication system handles this internally// when delivering subgroup objects to subscribers
Stream cancellation ensures subscribers always receive the most recent content with minimal latency, automatically dropping outdated frames during network congestion.
// Stop live source when no longer neededliveSource.stop()// Remove track from clientclient.removeTrack(track)// Announce namespace doneawait client.publishNamespaceDone(namespace)