useWebSocketStream
Stream the replies to a request into a value, such as a model's tokens into its answer, from createWebSocketRequests.
Usage
Parameters
Returns
UseWebSocketStreamResult<Req, T>:
Behavior
- One stream at a time.
start()aborts the stream before it, and replies to that one are ignored from then on. So a new question replaces the answer to the last one. - Cancels what it leaves.
abort(), a newstart(), unmounting and passing a differentrpcall stop the running stream, which sends the manager'scancelso the server stops generating. - Keeps partial answers. After
abort()or a failure,valuekeeps the replies that arrived. Onlystart()resets it. - Renders. It re-renders once per reply. With
batch: 'frame', it re-renders at most once per animation frame, which keeps fast token streams smooth. The end of the stream renders right away. Browsers pause frames in background tabs; the value catches up when the tab is visible again. - Idle timeout. The manager's
timeoutis the time to wait for each reply, including the first. Pass a longer one tostart()for models that think before answering. - Holds the connection while mounted, like the other hooks. A running stream also holds it.
- StrictMode-safe. If an effect calls
start(), StrictMode's double mount cancels the first stream and leaves one running. The first request still reaches the server, followed by its cancel. - Errors in
reduceend the stream with status'error', and sendcancel. - Server rendering. On the server,
valueisinitialandstatusis'idle'.