Skip to main content
WebSocketClient is an Effect service that wraps Convex’s ConvexClient. It provides the same query, mutation, and action methods as HttpClient, plus a reactiveQuery method that returns a Stream of live results. 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 the WebSocketClient layer by passing your Convex deployment URL.
The layer can then be provided to any Effect that uses the 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 the WebSocketClient service inside Effect.gen to call your functions with refs, the same way you would with HttpClient, @confect/react hooks, or @confect/test.
Each method returns an 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.
The underlying WebSocket subscription is cleaned up automatically when the stream’s scope ends (for example, when the consuming Effect is interrupted or when an operator like Stream.take completes).

Typed errors

When a ref’s spec declares an error 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.
For reactiveQuery, a typed failure terminates the Stream with the decoded value in its error channel—the subscription does not stay open across failures. Recover with Stream.catchTag (or any other Stream error combinator) to keep the stream alive across typed failures.

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.
The optional second argument is a function that receives the current isAuthenticated status and returns an Effect to run whenever the authentication state changes.

Running programs

Provide the WebSocketClient layer when running your program.

Differences from ConvexClient