DEV Community

Cao Wen
Cao Wen

Posted on

100 concurrent viewers is a chat problem, not a video problem

Every live shopping demo works with three viewers. At 100, the video is usually still fine. What breaks is the side channel: chat, presence, product pins, and the reconnect storm after a deploy.

Video is a fan-out you can cache

One RTMP/HTTP-FLV ingest into SRS. Every viewer pulls the same stream. No per-viewer transcode, no per-viewer process. CPU stays roughly flat as viewers join; bandwidth grows linearly. The failure modes are mundane: a bad cache header, a proxy that buffers, a player that keeps the last frame after the encoder reconnects.

Chat is a connection fan-out

Every viewer opens a WebSocket for chat, presence, and product pins. 100 sockets is not a lot. What hurts is what happens around them:

  • A deploy drops all sockets at once; 100 clients reconnect in the same second.
  • One slow consumer backs up the fan-out queue.
  • A new viewer asks for history and gets the entire stream.

The fix is boring: bounded history, drop-oldest backpressure, jittered reconnect, and a heartbeat that closes dead sockets.

Product pins need ordering

A pinned product is a small state machine. Two events can arrive out of order after a reconnect. If the client applies the older one last, the stream shows the wrong product. Put a monotonic id on every event and ignore anything older than the last applied id.

let lastId = 0;

function onEvent(event) {
  if (event.id <= lastId) return;
  lastId = event.id;
  apply(event);
}
Enter fullscreen mode Exit fullscreen mode

Reconnect is the real test

The encoder will drop. The player will reconnect. The UI must say "reconnecting" instead of pretending the last frame is live. The cart must survive the reconnect. The chat should backfill a bounded window, not the whole session.

We test this by killing the ingest mid-stream and checking three things: the player state, the pinned product, and the cart. If any of them resets, the widget is not ready.

What we changed

We separated video from events. Video is a pull; events are one WebSocket. Chat history is capped. Reconnects use jitter. Product pins are idempotent. None of this is exotic; it is just the part that does not fit in a demo.

If you run Shopify or WooCommerce, the widget is free up to 100 concurrent viewers, 0% commission. Setup notes: https://durianlive.com/?utm_source=devto&utm_medium=article

Top comments (0)