Skip to main content

Class: BaseConvexClient

For AI agents: see llms.txt for the complete documentation index. Markdown versions are available by adding .md to a page URL or requesting Accept: text/markdown.

browser.BaseConvexClient

Low-level client for directly integrating state management libraries with Convex.

Most developers should use higher level clients, like the ConvexHttpClient or the React hook based ConvexReactClient.

Constructors​

constructor​

• new BaseConvexClient(address, onTransition, options?)

Parameters​

NameTypeDescription
addressstringThe url of your Convex deployment, often provided by an environment variable. E.g. https://small-mouse-123.convex.cloud.
onTransition(updatedQueries: QueryToken[]) => voidA callback receiving an array of query tokens corresponding to query results that have changed -- additional handlers can be added via addOnTransitionHandler.
options?BaseConvexClientOptionsSee BaseConvexClientOptions for a full description.

Defined in​

browser/sync/client.ts:293

Accessors​

url​

• get url(): string

Return the address for this client, useful for creating a new client.

Not guaranteed to match the address with which this client was constructed: it may be canonicalized.

Returns​

string

Defined in​

browser/sync/client.ts:1055

Methods​

getMaxObservedTimestamp​

▸ getMaxObservedTimestamp(): undefined | Long

Returns​

undefined | Long

Defined in​

browser/sync/client.ts:554


addOnTransitionHandler​

▸ addOnTransitionHandler(fn): () => boolean

Add a handler that will be called on a transition.

Any external side effects (e.g. setting React state) should be handled here.

Parameters​

NameType
fn(transition: Transition) => void

Returns​

fn

▸ (): boolean

Returns​

boolean

Defined in​

browser/sync/client.ts:633


getCurrentAuthClaims​

▸ getCurrentAuthClaims(): undefined | { token: string ; decoded: Record<string, any> }

Get the current JWT auth token and decoded claims.

Returns​

undefined | { token: string ; decoded: Record<string, any> }

Defined in​

browser/sync/client.ts:642


setAuth​

▸ setAuth(fetchToken, onChange, onRefreshChange?): void

Set the authentication token to be used for subsequent queries and mutations. fetchToken will be called automatically again if a token expires. fetchToken should return null if the token cannot be retrieved, for example when the user's rights were permanently revoked.

Parameters​

NameTypeDescription
fetchTokenAuthTokenFetcheran async function returning the JWT-encoded OpenID Connect Identity Token
onChange(isAuthenticated: boolean) => voida callback that will be called when the authentication status changes
onRefreshChange?(isRefreshing: boolean) => voida callback called with true when the socket is paused to fetch a replacement token after a server rejection, and false when refresh completes

Returns​

void

Defined in​

browser/sync/client.ts:668


hasAuth​

▸ hasAuth(): boolean

Returns​

boolean

Defined in​

browser/sync/client.ts:680


clearAuth​

▸ clearAuth(): void

Returns​

void

Defined in​

browser/sync/client.ts:690


subscribe​

▸ subscribe(name, args?, options?): Object

Subscribe to a query function.

Whenever this query's result changes, the onTransition callback passed into the constructor will be called.

Parameters​

NameTypeDescription
namestringThe name of the query.
args?Record<string, Value>An arguments object for the query. If this is omitted, the arguments will be {}.
options?SubscribeOptionsA SubscribeOptions options object for this query.

Returns​

Object

An object containing a QueryToken corresponding to this query and an unsubscribe callback.

NameType
queryTokenQueryToken
unsubscribe() => void

Defined in​

browser/sync/client.ts:709


localQueryResult​

▸ localQueryResult(udfPath, args?): undefined | Value

A query result based only on the current, local state.

The only way this will return a value is if we're already subscribed to the query or its value has been set optimistically.

Parameters​

NameType
udfPathstring
args?Record<string, Value>

Returns​

undefined | Value

Defined in​

browser/sync/client.ts:742


queryJournal​

▸ queryJournal(name, args?): undefined | QueryJournal

Retrieve the current QueryJournal for this query function.

If we have not yet received a result for this query, this will be undefined.

Parameters​

NameTypeDescription
namestringThe name of the query.
args?Record<string, Value>The arguments object for this query.

Returns​

undefined | QueryJournal

The query's QueryJournal or undefined.

Defined in​

browser/sync/client.ts:795


connectionState​

▸ connectionState(): ConnectionState

Get the current ConnectionState between the client and the Convex backend.

Returns​

ConnectionState

The ConnectionState with the Convex backend.

Defined in​

browser/sync/client.ts:810


subscribeToConnectionState​

▸ subscribeToConnectionState(cb): () => void

Subscribe to the ConnectionState between the client and the Convex backend, calling a callback each time it changes.

Subscribed callbacks will be called when any part of ConnectionState changes. ConnectionState may grow in future versions (e.g. to provide a array of inflight requests) in which case callbacks would be called more frequently.

Parameters​

NameType
cb(connectionState: ConnectionState) => void

Returns​

fn

An unsubscribe function to stop listening.

▸ (): void

Subscribe to the ConnectionState between the client and the Convex backend, calling a callback each time it changes.

Subscribed callbacks will be called when any part of ConnectionState changes. ConnectionState may grow in future versions (e.g. to provide a array of inflight requests) in which case callbacks would be called more frequently.

Returns​

void

An unsubscribe function to stop listening.

Defined in​

browser/sync/client.ts:856


mutation​

▸ mutation(name, args?, options?): Promise<any>

Execute a mutation function.

Parameters​

NameTypeDescription
namestringThe name of the mutation.
args?Record<string, Value>An arguments object for the mutation. If this is omitted, the arguments will be {}.
options?MutationOptionsA MutationOptions options object for this mutation.

Returns​

Promise<any>

  • A promise of the mutation's result.

Defined in​

browser/sync/client.ts:876


action​

▸ action(name, args?): Promise<any>

Execute an action function.

Parameters​

NameTypeDescription
namestringThe name of the action.
args?Record<string, Value>An arguments object for the action. If this is omitted, the arguments will be {}.

Returns​

Promise<any>

A promise of the action's result.

Defined in​

browser/sync/client.ts:997


close​

▸ close(): Promise<void>

Close any network handles associated with this client and stop all subscriptions.

Call this method when you're done with an BaseConvexClient to dispose of its sockets and resources.

Returns​

Promise<void>

A Promise fulfilled when the connection has been completely closed.

Defined in​

browser/sync/client.ts:1044