createWebSocket
Create a WebSocket client to share across components or use outside React.
Available from both react-websocket and react-websocket/core.
Usage
Create clients at module level, outside components:
Then use it from any component with useWebSocket(live), useWebSocketEvent(live, …) or useWebSocketStatus(live), or directly:
createWebSocket inside a component body creates a new client on every render. Create it at module level, or use useWebSocket(url) for per-component connections.
Parameters
The In type is inferred from options.schema when you pass one.
Returns
Lifecycle
A client is lazy. Creating one opens nothing. It connects when:
- a hook using it mounts (or
client.acquire()is called), client.readyis read, orclient.connect()is called.
It closes one tick after the last hook unmounts (or the last acquire() is released), and its status returns to 'idle'. A connection started by reading ready that nothing holds closes a few seconds after it opens. Calling connect() opts out of that: the client then stays connected, reconnecting as needed, until you call close().
The full picture, including backoff and heartbeats, is in Reconnecting and heartbeats.
Client methods
Full signatures are in WebSocketClient.