Polling in React: When Smart Short-Polling Beats WebSocket Complexity
Why not every real-time feature needs WebSockets and how to build resilient, auto-pausing polling hooks in React 19.
Real-time UI updates — like tracking a vehicle inspection status, payment confirmation, or export file generation — are often over-engineered with full WebSocket infrastructure. For operations with short lifespans or sporadic updates, smart HTTP short-polling is significantly simpler, stateless, and easier to scale horizontally behind standard CDNs.
import { useEffect, useRef } from "react";
export function useSmartPolling(callback: () => Promise<void>, intervalMs = 3000, enabled = true) {
const savedCallback = useRef(callback);
savedCallback.current = callback;
useEffect(() => {
if (!enabled) return;
let timeoutId: NodeJS.Timeout;
const tick = async () => {
// Pause polling if user is in another browser tab
if (!document.hidden) {
try {
await savedCallback.current();
} catch (err) {
console.error("Polling error:", err);
}
}
timeoutId = setTimeout(tick, intervalMs);
};
timeoutId = setTimeout(tick, intervalMs);
return () => clearTimeout(timeoutId);
}, [intervalMs, enabled]);
}Click the button below to test headless modal composition:
The Hidden Cost of Persistent WebSocket Connections
WebSockets require stateful sticky sessions, complex reconnection backoff logic, and dedicated gateway scaling. If an API server restarts, thousands of clients reconnect simultaneously, creating a thundering herd problem.
A tab-aware polling hook, by contrast, automatically suspends execution when the user switches tabs (`document.hidden`), slashing unnecessary API traffic by over 60%.
“Choose boring technology when possible. A 3-second adaptive poll that pauses in background tabs is virtually indistinguishable from a WebSocket for status trackers.”
Technical Summary
Evaluate your real-time requirements pragmatically. Save WebSockets for collaborative multi-cursor editors and chat systems; use adaptive polling for status lifecycles.
Sign up to receive a weekly recap from Emicraft
Deep-dives on software architecture, design systems, and scaling senior engineering squads. No marketing fluff — only production insights.
Timilehin Aliyu
Software Engineer
Software engineer building resilient web frontends, offline-capable PWAs, AI compilers, and financial transaction engines at Emicraft.