useWebSocket
React hook to manage WebSocket connections with auto-reconnect, message handlers, and connection state callbacks.
Demo
Connectingecho.websocket.events
Send a message to see the echo
Usage
Simple — URL + optional onMessage callback:
Object form — full control:
Parameters
Simple form
| Parameter | Type | Description |
|---|---|---|
url | string | WebSocket server URL. |
onMessage | (event: MessageEvent) => void | Optional. Fires on each incoming message. |
Object form
| Property | Type | Default | Description |
|---|---|---|---|
url | string | — | WebSocket server URL. Changing this reconnects to the new URL. |
onMessage | (event: MessageEvent) => void | — | Fires on each incoming message. |
onOpen | (event: Event) => void | — | Fires when the connection opens. |
onClose | (event: CloseEvent) => void | — | Fires when the connection closes. |
onError | (event: Event) => void | — | Fires on connection error. |
reconnect | boolean | false | Automatically reconnect after the connection closes. |
reconnectDelay | number | 3000 | Delay in ms before attempting to reconnect. |
Return Values
| Property | Type | Description |
|---|---|---|
send | (data: string | Blob | ...) => void | Sends data. No-op if the socket is not open. |
disconnect | () => void | Closes the connection and cancels any pending reconnect. |
readyState | 0 | 1 | 2 | 3 | CONNECTING | OPEN | CLOSING | CLOSED. |
lastMessage | MessageEvent | null | The most recent message event. |