createWebSocketRequests
Send requests over a client and await their replies, or iterate over a stream of them, matched by an id the library generates.
Available from both react-websocket and react-websocket/core.
Usage
The guide is Real-time games.
Streaming replies
When one request gets many replies, such as tokens from a model, set done to recognize the last one and iterate with stream(). Set cancel to tell the server to stop when the loop exits early or the request is aborted.
In React, useWebSocketStream folds the replies into state. The guide is AI agents.
Parameters
Returns
WebSocketRequests<Req, In>:
request options
RequestOptions, for both request() and stream(), extends SendOptions:
Behavior
- Rejects, never hangs. A request rejects on timeout (a
DOMExceptionnamedTimeoutError), whenerrorreturns a value, when its signal aborts, when the connection it was sent on closes, onclose(), when the client gives up, and ondispose(). A stream throws in the same cases. - Lost connections. A reply can only arrive on the connection the request was sent on. When that connection closes, including through
reconnect(), the request rejects instead of waiting through the reconnect. - Queued while disconnected. A request made while connecting or reconnecting waits in the client's send queue and goes out when the socket opens. Its timeout runs from the call. With
volatile, or on aclosedclient, it rejects right away, and aclosedclient stays closed. So does a request dropped bymaxBufferedwhile open. If it's dropped from the queue before it's sent (replaced by a newer request with the samekey, evicted byqueue.limit, or older thanmaxAge), it rejects then. On a client created withqueue: false, the request is dropped and only rejects on timeout, so passvolatilethere. - Unique ids per client. Ids count up per client, shared by every manager on it, so two managers on one client never send the same id.
- Holds the connection. A pending request or stream holds the client like a mounted hook, so it works without React. The client is released when the request settles or the stream ends.
- Streams.
stream()sends right away, likerequest(). Replies that arrive before the loop reads them are buffered, so a slow loop misses nothing. A stream is read once, by one loop.request()withdoneset still resolves with the first reply. - Idle timeout. A stream's
timeoutrestarts with every reply, so a long answer never times out while replies keep coming. It also covers the wait for the first reply: a model that thinks before answering may need a longertimeout. - Failure order. When a stream fails, it yields the replies it already received, then throws. Aborting its signal is different: it throws
signal.reasonat the next read and discards what's buffered, so a Stop button takes effect at once. A failure nobody reads yet is kept for the next read, never reported as an unhandled rejection. - Cancel. With
cancelset, the message it builds is sent when this side gives up while the server may still be working: the signal aborts, the request times out, a loop over a stream exits early (break,return, or a throw in its body), and ondispose(). It isn't sent after the last reply, an error reply, a lost connection orclose(). A cancel for a request still in the send queue is queued right behind it, since a queued message can't be taken back. A cancel for a request already sent goes out only on that connection, and is dropped if it's closing. - Replies are messages. Replies also reach
onMessage,lastMessage,useWebSocketEventand state stores. A late reply to a request that already timed out is delivered there and ignored here. - Requires TypeScript 5.4+ for the inferred message types (it uses
NoInfer).