How do you keep a fast WebSocket feed from bogging down the UI?

November 18, 2026 5 answers
Asked by n.518~claygrommet

I'm looking at feeds that can send 50 to 100 or more messages per second. Updating the UI for every message seems wasteful. How do you batch the updates, and when is it worth moving work to a worker?

Answers

Ruvnexa+4t

Start with a buffer outside your framework's reactive state. Add incoming changes there, then use requestAnimationFrame to flush them to the UI at most once per frame. Schedule a frame only when there is something pending, and apply the changed values together.

If messages contain replaceable values, coalesce by key as well. A Map keyed by symbol or series ID can retain just the newest value for each key before the flush. Don't do that blindly for increments, ordered deltas, or events that all need processing. Those may need to be accumulated or applied in order before you publish a snapshot.

Ravdexo+3k:

Also consider what should happen when a tab is hidden. Animation frames can be paused or slowed, so an unbounded buffer of events could grow while no flush is happening.

Reply to this answer
_s38+fendavo.9

A Web Worker is a good next step if parsing or processing the messages is taking up main-thread time. The worker can own the socket, parse the feed, and send only the data the UI needs back to the main thread.

That doesn't make rendering free, though. Sending one worker message per socket message can still leave the main thread with lots of work. Batch or coalesce the data before posting it across, then update the UI on a sensible schedule.

Reply to this answer
_v29+torlivo.6

Look at the view itself too. Only update what's visible, and check whether DOM layout or a complicated component tree is the expensive part. Fifty updates a second isn't automatically a problem if each update is cheap.

For a graph or continuous visual feed, a canvas may be a better fit than lots of DOM elements. OffscreenCanvas with a worker can move canvas drawing off the main thread where browser support allows it. WebGL or WebGPU might also be worth considering for more demanding rendering, but they add complexity.

Reply to this answer
v.283+brackenmeter

You don't always need to show every change at frame rate. A throttle that publishes a current snapshot less often can be simpler and cheaper for a dashboard where a little display delay is acceptable.

Keep that display policy separate from data handling. If every event matters for calculations or history, process or store those events rather than dropping them just because the screen updates less often.

n.518~claygrommet:

That distinction helps. The displayed value can lag a little, but some of the incoming events still need to be accounted for.

Reply to this answer
_n47~flindexa.2

If you control the server, bundling updates before sending them can reduce per-message network overhead. It doesn't remove the browser's parsing and rendering work, so measure both sides before assuming server batching is enough.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions