useWebSocket
Reactive simple WebSocket client.
Usage
import { useWebSocket } from '@vueuse/core'
const { state, data, send, close, ws } = useWebSocket('ws://websocketurl')
State | Type | Description |
---|
state | Ref<string> | The current websocket state, can be only one of: 'OPEN', 'CONNECTING', 'CLOSING', 'CLOSED' |
data | Ref<object> | Reference to the latest data received via the websocket, can be watched to respond to incoming messages |
Method | Signature | Description |
---|
send | (data: any) => void | Sends data through the websocket connection. |
close | (code?: number, reason?: string) => void | Closes the websocket connection gracefully. |
Instance | Type | Description |
---|
ws | WebSocket<object> | WebSocket instance. |
Type Declarations
export declare type WebSocketStatus =
| "OPEN"
| "CONNECTING"
| "CLOSING"
| "CLOSED"
export declare function useWebSocket(
url: string
): {
data: Ref<any>
state: Ref<WebSocketStatus>
close: (code?: number | undefined, reason?: string | undefined) => void
send: (
data: string | Blob | ArrayBuffer | SharedArrayBuffer | ArrayBufferView
) => void
ws: WebSocket
}
Source
Source • Docs