principletypescriptTip
Live Streaming Integration Requires HLS/DASH for Broad Compatibility, WebRTC for Sub-Second Latency
Viewed 0 times
live streamingHLSLL-HLSWebRTCvideo latencySFUadaptive bitrate
Problem
A live streaming feature is built using HLS which has 15–30 second latency by default, making it unusable for interactive live sessions (Q&A, auctions, gaming commentary).
Solution
Use Low-Latency HLS (LL-HLS) for 2–5 second latency with broad CDN support, or WebRTC for sub-second latency in small-audience interactive scenarios. Choose based on audience size and interactivity requirements.
// HLS.js client for LL-HLS
import Hls from 'hls.js';
function setupLowLatencyHLS(videoEl: HTMLVideoElement, streamUrl: string) {
if (!Hls.isSupported()) {
videoEl.src = streamUrl; // Safari native HLS
return;
}
const hls = new Hls({
lowLatencyMode: true,
backBufferLength: 10,
liveMaxLatencyDuration: 5, // Target <=5s latency
liveSyncDuration: 2
});
hls.loadSource(streamUrl);
hls.attachMedia(videoEl);
videoEl.play();
return () => hls.destroy();
}Why
Standard HLS segments are 6–10 seconds. LL-HLS uses 200ms partial segments and HTTP/2 push to reduce latency to 2–5 seconds. WebRTC achieves <500ms but requires a media server (SFU) for more than ~20 concurrent viewers.
Gotchas
- LL-HLS requires HTTP/2 and proper CDN support — most CDNs support it but verify your specific provider.
- WebRTC SFUs (Janus, mediasoup, LiveKit) add significant infrastructure complexity and cost.
- For >1000 viewers, WebRTC fan-out is not feasible — use HLS with a CDN even at slightly higher latency.
- DASH (Dynamic Adaptive Streaming over HTTP) is the MPEG standard equivalent to HLS — prefer HLS for Apple devices.
Revisions (0)
No revisions yet.