A real-time dashboard updates for a while and then silently stops because the underlying WebSocket connection dropped without triggering an automatic reconnect handler, or because high-frequency telemetry flooded the client memory buffer. Implementing ping/pong heartbeats and debounced state batching keeps live streams stable indefinitely.
Targeted Code Solution
hooks/useResilientWebSocket.tshooks/useResilientWebSocket.tstypescript
import { useEffect, useRef, useState, useCallback } from 'react';
export function useResilientWebSocket(url: string) {
const [data, setData] = useState<any[]>([]);
const wsRef = useRef<WebSocket | null>(null);
const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const bufferRef = useRef<any[]>([]);
const connect = useCallback(() => {
const ws = new WebSocket(url);
wsRef.current = ws;
ws.onmessage = (event) => {
const point = JSON.parse(event.data);
bufferRef.current.push(point);
};
ws.onclose = () => {
// Reconnect with backoff
reconnectTimeoutRef.current = setTimeout(() => {
connect();
}, 3000);
};
ws.onerror = () => ws.close();
}, [url]);
useEffect(() => {
connect();
// Flush incoming buffer to React state only once every 250ms (batching)
const interval = setInterval(() => {
if (bufferRef.current.length > 0) {
setData((prev) => [...prev, ...bufferRef.current].slice(-100)); // Keep last 100 points
bufferRef.current = [];
}
}, 250);
return () => {
clearInterval(interval);
if (reconnectTimeoutRef.current) clearTimeout(reconnectTimeoutRef.current);
if (wsRef.current) wsRef.current.close();
};
}, [connect]);
return data;
}
Need this resolved on your app?
If this error is blocking your release build or production pipeline, Syed Alijah Muhammad can diagnose and deploy a clean fix. Send the repository access, error trace, and platform details.