WebSocketClient is an Effect service that wraps Convex’s ConvexClient. It provides the same query, mutation, and action methods as HttpClient, plus reactiveQuery and reactiveQueryResult methods that return Stream values. It works in any JavaScript environment that supports WebSocket.
The WebSocket connection is managed as a scoped resource—it is opened when the layer is provided and closed automatically when the scope ends.
Setup
Create theWebSocketClient layer by passing your Convex deployment URL.
WebSocketClient service. The underlying WebSocket connection is closed automatically when the layer’s scope ends—there is no need to close it manually.
Calling functions
Use theWebSocketClient service inside Effect.gen to call your functions with refs, the same way you would with HttpClient, @confect/react hooks, or @confect/test.
Effect that can fail with WebSocketClientError (wrapping transport-level errors) or ParseResult.ParseError (if schema encoding or decoding fails).
Reactive queries
reactiveQuery subscribes to a query over the WebSocket connection and returns a Stream that emits a new value whenever the query result changes on the server.
Stream.take completes).
Typed errors
When a ref’s spec declares anerror schema, the decoded error is added to the error channel of query, mutation, action, and reactiveQuery alongside WebSocketClientError and ParseError. See Error Handling for how to declare error schemas.
reactiveQuery uses the ordinary Stream error channel, so a query failure
terminates that stream. Use reactiveQueryResult when an error should instead
be one event in a continuing subscription. It emits
Result<Returns, TypedError | WebSocketClientError | SchemaError> values and
keeps the underlying Convex listener open, allowing a later query result to
recover naturally:
Authentication
Set the authentication token provider before making authenticated requests.setAuth accepts an Effect-returning function that is called whenever a token is needed or expires.
isAuthenticated status and returns an Effect to run whenever the authentication state changes.
Running programs
Provide theWebSocketClient layer when running your program.