Registry / communication / react-native-reconnecting-websocket

react-native-reconnecting-websocket

JSON →
library2.0.0jsnpmunverified

A drop-in replacement for the standard WebSocket API in React Native that automatically reconnects when the connection drops. This library wraps the native WebSocket module and provides exponential backoff reconnection logic, configurable intervals, and max attempts. It is API-compatible with the native WebSocket, so you can replace `new WebSocket(url)` with `new ReconnectingWebSocket(url)` without changing event listeners. Current stable version is 2.0.0. Release cadence is low; last release was in 2021. Key differentiators: designed specifically for React Native (no browser dependencies), supports headers (including Cookie and custom Origin), and exposes options like reconnectInterval, maxReconnectInterval, reconnectDecay, timeoutInterval, and maxReconnectAttempts.

npm install react-native-reconnecting-websocket
INSTALL
IMPORT
SIG · REACT-NATIVE-RECON
R
react-native-reconnecting-websocket
communicationjavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import ReconnectingWebSocket from 'react-native-reconnecting-websocket'
import { ReconnectingWebSocket } from 'react-native-reconnecting-websocket'
Default export only; named import is incorrect.
ReconnectingWebSocket constructor
const ws = new ReconnectingWebSocket('ws://example.com')
const ws = new WebSocket('ws://example.com')
Replace `new WebSocket` with `new ReconnectingWebSocket` for auto-reconnect behavior.
CommonJS require
const ReconnectingWebSocket = require('react-native-reconnecting-websocket')
const { ReconnectingWebSocket } = require('react-native-reconnecting-websocket')
CommonJS users must assign the default export, not destructure.

Creates a ReconnectingWebSocket with custom reconnect options and demonstrates basic event handling (open, message, close).

import ReconnectingWebSocket from 'react-native-reconnecting-websocket'; const ws = new ReconnectingWebSocket('wss://echo.websocket.org', [], { reconnectInterval: 2000, maxReconnectAttempts: 5, }); ws.onopen = () => { console.log('Connected'); ws.send('Hello'); }; ws.onmessage = (e) => { console.log('Received:', e.data); }; ws.onclose = (e) => { console.log('Closed:', e.code, e.reason); }; // Close after 10 seconds setTimeout(() => ws.close(1000, 'Closing'), 10000);
Debug
Known issues
breakingIn v2.0.0, the `origin` option is deprecated; use `headers: { origin: ... }` instead.
fix
Pass origin under `headers` option: new ReconnectingWebSocket(url, null, { headers: { origin: '...' } })
affects: >=2.0.0
deprecatedThe `options` parameter `origin` is deprecated and may be removed in future versions.
fix
Move the `origin` property into a `headers` object.
affects: >=0.0.0
gotchaReconnectingWebSocket does not support browser `Event` interface; events are plain objects with `data`, `code`, `reason` etc.
fix
Access properties directly (e.g., `e.data`) instead of using `e.target` or other Event properties.
affects: >=0.0.0
gotchaThe library is React Native only; it will not work in a standard browser environment.
fix
Use a browser-compatible alternative like `reconnecting-websocket` for web apps.
affects: >=0.0.0
deprecatedThe `Cookie` header may be ignored on iOS due to platform restrictions.
fix
Use alternative authentication methods (e.g., token in URL or message) for iOS.
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'react-native-reconnecting-websocket'
Package not installed or dependency resolution failed in React Native environment.
fix
Run `npm install react-native-reconnecting-websocket` and ensure Metro bundler is restarted.
undefined is not an object (evaluating 'new ReconnectingWebSocket(...)')
Importing as named export instead of default.
fix
Change import to `import ReconnectingWebSocket from '...'` (no curly braces).
WebSocket is not defined
Using the library outside of React Native (e.g., in a browser or Node.js).
fix
Only use this package in a React Native app; for web, use a different reconnecting WebSocket library.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
react-nativerequiredRelies on the native WebSocket module provided by React Native.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
react-native-reconnecting-websocket — npm install react-native-reconnecting-websocket · libregistry