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 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).
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.
Differences from ConvexClient
ConvexClient | WebSocketClient |
|---|---|
Functions referenced via api.module.fn | Functions referenced via refs |
| Args passed directly to Convex as-is | Args are schema-encoded from Type to Encoded before sending |
| Return values received directly as-is | Return values are schema-decoded from Encoded to Type |
query / mutation / action return Promise | query / mutation / action return Effect with typed errors |
onUpdate uses callbacks and returns an unsubscribe function | reactiveQuery returns a Stream with automatic cleanup |
Must call close() manually | Connection closed automatically when the layerβs scope ends |
setAuth takes a Promise-returning callback | setAuth takes an Effect-returning function |