Higher-order components (HOCs) for integrating WebSocket connections into React applications. Current stable version is 3.1.0. It provides a declarative way to subscribe to WebSocket messages and events using React context and HOC composition. Key differentiators: explicit separation of WebSocket source (context provider), subscription creation (createSubscription), and connection injection (withWebSocket). Compared to raw WebSocket usage or hook-based libraries, this library uses a HOC pattern typical of class-component era React. It requires React 16.8.6+ and is Node 8+ compatible. Maintenance is minimal; last release was in 2019.
npm install react-with-websocketNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows HOC pattern: createSubscription with full lifecycle, then withWebSocket to inject WebSocket from context provider.
Consider using a hooks-based WebSocket library like 'react-use-websocket' for new projects.
See v2 migration guide: ensure mapEventToState is included as 5th arg even if undefined.
Implement your own reconnection logic for the WebSocket object before passing to provider.
Pass undefined as the 5th argument to createSubscription if not needed; do not skip the argument.
Ensure ws is created before render: const ws = useRef(null); useEffect(() => { ws.current = new WebSocket(url); }, []);Wrap components with <WebSocketContextProvider value={ws}> and ensure ws is defined.Use import { createSubscription } from 'react-with-websocket' instead of default import.