Live streaming requires different architecture than on-demand video. This guide covers building scalable live streaming platforms.
Architecture
Live streaming involves: capture → encode → transmit → deliver → play
WebRTC for Low Latency
Server Setup
typescript
import { RTCPeerConnection } from 'wrtc';
const peers = new Map<string, RTCPeerConnection>();
io.on('connection', (socket) => {
socket.on('join-stream', async () => {
const pc = new RTCPeerConnection({
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
});
peers.set(socket.id, pc);
pc.onicecandidate = (event) => {
socket.emit('ice-candidate', event.candidate);
};
});
});HLS Live Streaming
FFmpeg for Live Transcoding
bash
ffmpeg -i rtsp://camera-ip/stream \
-c:v libx264 -preset fast -tune zerolatency \
-c:a aac -b:a 128k \
-f hls -hls_time 2 -hls_list_size 5 \
-hls_flags delete_segments+append_list \
live/stream.m3u8Conclusion
Use WebRTC for interactive, low-latency streaming. Use HLS for one-to-many broadcasts. Combine both for optimal user experience.