Registry /
messaging / react-native-use-websocket
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.
useWebSocket
✓ import useWebSocket from 'react-native-use-websocket'
✗ import { useWebSocket } from 'react-native-use-websocket'
Default export only; named import will fail.
ReadyState
✓ import useWebSocket, { ReadyState } from 'react-native-use-websocket'
✗ const ReadyState = require('react-native-use-websocket').ReadyState
Named export available for TypeScript enum of connection states.
SendMessage
✓ import useWebSocket from 'react-native-use-websocket';
const { sendMessage } = useWebSocket(url);
✗ import { sendMessage } from 'react-native-use-websocket'
sendMessage is returned by the hook, not a standalone export.
Basic React Native component using useWebSocket to send messages and display connection status.
import React, { useState, useCallback } from 'react';
import { Button, Text } from 'react-native';
import useWebSocket, { ReadyState } from 'react-native-use-websocket';
export default function App() {
const [url] = useState('wss://echo.websocket.org');
const { sendMessage, lastMessage, readyState } = useWebSocket(url, {
shouldReconnect: (closeEvent) => true,
});
const handleSend = useCallback(() => sendMessage('Hello'), [sendMessage]);
const status = {
[ReadyState.CONNECTING]: 'Connecting',
[ReadyState.OPEN]: 'Open',
[ReadyState.CLOSING]: 'Closing',
[ReadyState.CLOSED]: 'Closed',
[ReadyState.UNINSTANTIATED]: 'Uninstantiated',
}[readyState];
return (
<>
<Button onPress={handleSend} title="Send Hello" disabled={readyState !== ReadyState.OPEN} />
<Text>Status: {status}</Text>
{lastMessage && <Text>Last: {lastMessage.data}</Text>}
</>
);
}
Errors
Common errors & fixes
TypeError: Cannot destructure property 'sendMessage' of 'undefined' or 'null'
Named import instead of default import: `import { useWebSocket }` returns undefined.
fixChange to: `import useWebSocket from 'react-native-use-websocket'`
WebSocket is closed before the connection is established.
Component unmounts quickly or url changes trigger new connection.
fixEnsure url is stable or add cleanup logic: return () => {} in useEffect. Audit
Dependencies
reactrequiredpeer dependency, requires React 16.8+ for hooks
react-nativerequiredpeer dependency, core environment