Handling source failover during a live match
Upstream sports feeds drop mid-match. Design the client so a failed source is a two-second interruption rather than a dead player: ranking sources, switching without a refetch, and what to show while it happens.
Assume the source will fail
Upstream signals are third-party. They drop, get throttled, degrade in quality, or disappear entirely in the middle of a match, and no amount of edge infrastructure prevents that — the edge can only make the switch fast and invisible.
This is why the stream resolver returns an array rather than a single URL. A client that reads sources[0] and discards the rest has thrown away the only thing that makes recovery possible without a user-visible failure.
Rank sources before you need them
Rank the array once at resolve time rather than deciding under pressure. The checkedAt timestamp tells you how recently each source passed a health probe, which is the strongest available signal. Beyond that, sources whose names indicate a higher tier broadcaster tend to be more stable.
Keeping the ranked list in memory means a failover is an array index increment, not a network round trip.
function rankSources(sources) {
return [...sources].sort((a, b) => {
// Most recently verified first.
const freshness =
new Date(b.checkedAt).getTime() - new Date(a.checkedAt).getTime();
if (Math.abs(freshness) > 30_000) return freshness;
// Within the same probe window, prefer explicit HD labelling.
const hd = (s) => (/\b(1080|fhd|hd)\b/i.test(s.name) ? 1 : 0);
return hd(b) - hd(a);
});
} Switch without restarting the player
Destroying the Hls instance and building a new one costs a visible black frame and loses the buffer. Calling loadSource() on the existing instance reuses the media element and the decoder, and the viewer sees a short freeze instead of a reset.
Track attempts per source so a flapping feed cannot trap the loop. Three failures on one source means move on; exhausting the array means the match genuinely has no working signal, which is worth telling the user plainly rather than spinning forever.
class StreamSession {
constructor(hls, sources) {
this.hls = hls;
this.sources = rankSources(sources);
this.index = 0;
this.attempts = 0;
}
current() {
return this.sources[this.index];
}
failOver() {
this.attempts += 1;
// Give a source three chances before abandoning it.
if (this.attempts < 3) {
this.hls.loadSource(this.current().url);
return true;
}
this.index += 1;
this.attempts = 0;
if (this.index >= this.sources.length) return false; // exhausted
this.hls.loadSource(this.current().url);
return true;
}
} What the viewer should see
A failover that takes two seconds and shows nothing reads as a bug. The same two seconds with a quiet "switching source" indicator reads as the system working, and the difference in support tickets is substantial.
Keep the message neutral and avoid naming the broadcaster. Do not surface the attempt count or the source name — it means nothing to a viewer and leaks more about your upstream than you want in a screenshot.