EmicraftEngineering Firm

Appearance

Start a project
Timilehin AliyuJuly 30, 2026
REACT, JAVASCRIPT, FRONTEND, PERFORMANCE, HOOKS, TIPS

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.

Tab-Aware Adaptive Polling Hook in Reacttypescript
// Source Code✓ Type-Safe
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]);
}
Interactive PreviewLive State

Click the button below to test headless modal composition:

Rendered at 60 FPSZero Latency

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.

✓ Delivered every Tuesday✓ Unsubscribe anytime
TA

Timilehin Aliyu

Software Engineer

Software engineer building resilient web frontends, offline-capable PWAs, AI compilers, and financial transaction engines at Emicraft.