Skip to content

useWebSocket ​

Category
Network

Hook that connects to a WebSocket server and handles incoming and outgoing messages

typescript
import { useWebSocket } from '@siberiacancode/reactuse';

Usage ​

typescript
const { status, close, send, open, client } = useWebSocket('url');

Demo ​

Api ​

Parameters

NameTypeDefaultNote
urlUseWebSocketUrl-The URL of the WebSocket server
options.onConnected?(webSocket: WebSocket) => void-The callback function that is called when the WebSocket connection is established
options.onDisconnected?(event: CloseEvent, webSocket: WebSocket) => void-The callback function that is called when the WebSocket connection is closed
options.onError?(event: Event, webSocket: WebSocket) => void-The callback function that is called when an error occurs
options.onMessage?(event: MessageEvent, webSocket: WebSocket) => void-The callback function that is called when a message is received
options.retry?boolean | number-The number of times to retry the connection
options.protocols?Array<'soap' | 'wasm'>-The list of protocols to use

Contributors ​

Source • Demo