A production-ready React library providing composable hooks for WebSocket and Server-Sent Events (SSE) with auto-reconnect, heartbeat, typed connection states, and browser network awareness. Current stable version is 2.0.2, released in 2024, with a release cadence of approximately monthly. Key differentiators include discriminated connection snapshots (idle, connecting, open, reconnecting, closing, closed, error), exponential backoff with jitter, heartbeat with ack matching and latency measurement, environment-aware connection gating for offline state and background tabs, and zero runtime dependencies beyond React. Ships TypeScript types, supports React 18+, and is SSR- and Strict Mode-safe.
npm install react-realtime-hooksNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows a React component using useWebSocket with reconnection, heartbeat, and connection gating from useConnectionGate. Includes TypeScript typing for send and parse messages.
Update imports and hook usage to match new API. Refer to the migration guide in the GitHub repository. For heartbeat, use the new 'heartbeat' option object instead of previous callback-based approach.
Use ESM imports: import { ... } from 'react-realtime-hooks'. In Node.js, set "type": "module" in package.json or use dynamic import.Remove standalone useReconnect calls and configure reconnect within the transport hook options.
Avoid setting global state in onOpen that expects one-time initialization. Use useEffect for such side effects with appropriate dependencies.
Install React 18 or 19: npm install react@18 react-dom@18
Use ESM import syntax: import { useWebSocket } from 'react-realtime-hooks'; Or set "type": "module" in your package.json.Upgrade to v2.0.0 or later: npm install react-realtime-hooks@latest
Ensure you pass a valid WebSocket URL string (e.g., 'wss://example.com/socket') to useWebSocket. Use process.env variables with a fallback.