useWebSocket
Connects a WebSocket route from a component: data holds the last message, status the connection's state, and send writes to it.
Connects a WebSocket route from a component: data holds the last message, status the connection's
state, and send writes to it. The socket closes when the component is disposed.
Import#
ts
import { useWebSocket } from "@shadow-js/dusk/client";
Signature#
ts
function useWebSocket<T = unknown>(
url: string,
options: UseWebSocketOptions<T> = {},
): WebSocketHandle<T>
Parameters#
| Name | Description |
|---|---|
url |
The socket URL, or a path on this origin. |
options |
Parse, subprotocols, reconnection and the socket constructor. |
Returns#
The reactive handle.
Examples#
ts
const chat = useWebSocket<ChatMessage>("/chat");
chat.send(JSON.stringify({ text: "hi" }));