> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Class: BijectionClient

> Subscribes to Bijection query functions and executes mutations and actions over a WebSocket.

[browser](/api/modules/browser).BijectionClient

Subscribes to Bijection query functions and executes mutations and actions over a WebSocket.

Optimistic updates for mutations are not provided for this client.
Third party clients may choose to wrap [BaseBijectionClient](/api/classes/browser.BaseBijectionClient) for additional control.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const client = new BijectionClient("https://happy-otter-123.bijection.cloud");
const unsubscribe = client.onUpdate(api.messages.list, {}, (messages) => {
  console.log(messages[0].body);
});
```

## Constructors

### constructor

• **new BijectionClient**(`address`, `options?`)

Construct a client and immediately initiate a WebSocket connection to the passed address.

#### Parameters

| Name | Type |
| :- | :- |
| `address` | `string` |
| `options` | [`BijectionClientOptions`](/api/modules/browser#bijectionclientoptions) |

## Accessors

### closed

• `get` **closed**(): `boolean`

Once closed no registered callbacks will fire again.

#### Returns

`boolean`

***

### client

• `get` **client**(): [`BaseBijectionClient`](/api/classes/browser.BaseBijectionClient)

#### Returns

[`BaseBijectionClient`](/api/classes/browser.BaseBijectionClient)

***

### disabled

• `get` **disabled**(): `boolean`

#### Returns

`boolean`

## Methods

### onUpdate

▸ **onUpdate**\<`Query`>(`query`, `args`, `callback`, `onError?`): `Unsubscribe`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>>

Call a callback whenever a new result for a query is received. The callback
will run soon after being registered if a result for the query is already
in memory.

The return value is an Unsubscribe object which is both a function
an an object with properties. Both of the patterns below work with this object:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// call the return value as a function
const unsubscribe = client.onUpdate(api.messages.list, {}, (messages) => {
  console.log(messages);
});
unsubscribe();

// unpack the return value into its properties
const {
  getCurrentValue,
  unsubscribe,
} = client.onUpdate(api.messages.list, {}, (messages) => {
  console.log(messages);
});
```

#### Type parameters

| Name | Type |
| :- | :- |
| `Query` | extends [`FunctionReference`](/api/modules/server#functionreference)\<`"query"`> \| [`FunctionReference_future`](/api/modules/server#functionreference_future)\<`"query"`> |

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `query` | `Query` | A [FunctionReference](/api/modules/server#functionreference) for the public query to run. |
| `args` | [`FunctionArgs`](/api/modules/server#functionargs)\<`Query`> | The arguments to run the query with. |
| `callback` | (`result`: [`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>) => `unknown` | Function to call when the query result updates. |
| `onError?` | (`e`: `Error`) => `unknown` | Function to call when the query result updates with an error. If not provided, errors will be thrown instead of calling the callback. |

#### Returns

`Unsubscribe`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>>

an Unsubscribe function to stop calling the onUpdate function.

***

### onPaginatedUpdate\_experimental

▸ **onPaginatedUpdate\_experimental**\<`Query`>(`query`, `args`, `options`, `callback`, `onError?`): `Unsubscribe`\<`PaginatedQueryResult`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>\[]>>

Call a callback whenever a new result for a paginated query is received.

This is an experimental preview: the final API may change.
In particular, caching behavior, page splitting, and required paginated query options
may change.

#### Type parameters

| Name | Type |
| :- | :- |
| `Query` | extends [`FunctionReference`](/api/modules/server#functionreference)\<`"query"`> \| [`FunctionReference_future`](/api/modules/server#functionreference_future)\<`"query"`> |

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `query` | `Query` | A [FunctionReference](/api/modules/server#functionreference) for the public query to run. |
| `args` | [`FunctionArgs`](/api/modules/server#functionargs)\<`Query`> | The arguments to run the query with. |
| `options` | `Object` | Options for the paginated query including initialNumItems and id. |
| `options.initialNumItems` | `number` | - |
| `callback` | (`result`: [`PaginationResult`](/api/interfaces/server.PaginationResult)\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>>) => `unknown` | Function to call when the query result updates. |
| `onError?` | (`e`: `Error`) => `unknown` | Function to call when the query result updates with an error. |

#### Returns

`Unsubscribe`\<`PaginatedQueryResult`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>\[]>>

an Unsubscribe function to stop calling the callback.

***

### close

▸ **close**(): `Promise`\<`void`>

#### Returns

`Promise`\<`void`>

***

### getAuth

▸ **getAuth**(): `undefined` | \{ `token`: `string` ; `decoded`: `Record`\<`string`, `any`>  }

Get the current JWT auth token and decoded claims.

#### Returns

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

***

### setAuth

▸ **setAuth**(`fetchToken`, `onChange?`): `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

| Name | Type | Description |
| :- | :- | :- |
| `fetchToken` | [`AuthTokenFetcher`](/api/modules/browser#authtokenfetcher) | an async function returning the JWT (typically an OpenID Connect Identity Token) |
| `onChange?` | (`isAuthenticated`: `boolean`) => `void` | a callback that will be called when the authentication status changes |

#### Returns

`void`

***

### mutation

▸ **mutation**\<`Mutation`>(`mutation`, `args`, `options?`): `Promise`\<`Awaited`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Mutation`>>>

Execute a mutation function.

#### Type parameters

| Name | Type |
| :- | :- |
| `Mutation` | extends [`FunctionReference`](/api/modules/server#functionreference)\<`"mutation"`> \| [`FunctionReference_future`](/api/modules/server#functionreference_future)\<`"mutation"`> |

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `mutation` | `Mutation` | A [FunctionReference](/api/modules/server#functionreference) for the public mutation to run. |
| `args` | [`FunctionArgs`](/api/modules/server#functionargs)\<`Mutation`> | An arguments object for the mutation. |
| `options?` | [`MutationOptions`](/api/interfaces/browser.MutationOptions) | A [MutationOptions](/api/interfaces/browser.MutationOptions) options object for the mutation. |

#### Returns

`Promise`\<`Awaited`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Mutation`>>>

A promise of the mutation's result.

***

### action

▸ **action**\<`Action`>(`action`, `args`): `Promise`\<`Awaited`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Action`>>>

Execute an action function.

#### Type parameters

| Name | Type |
| :- | :- |
| `Action` | extends [`FunctionReference`](/api/modules/server#functionreference)\<`"action"`> \| [`FunctionReference_future`](/api/modules/server#functionreference_future)\<`"action"`> |

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `action` | `Action` | A [FunctionReference](/api/modules/server#functionreference) for the public action to run. |
| `args` | [`FunctionArgs`](/api/modules/server#functionargs)\<`Action`> | An arguments object for the action. |

#### Returns

`Promise`\<`Awaited`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Action`>>>

A promise of the action's result.

***

### query

▸ **query**\<`Query`>(`query`, `args`): `Promise`\<`Awaited`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>>>

Fetch a query result once.

#### Type parameters

| Name | Type |
| :- | :- |
| `Query` | extends [`FunctionReference`](/api/modules/server#functionreference)\<`"query"`> \| [`FunctionReference_future`](/api/modules/server#functionreference_future)\<`"query"`> |

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `query` | `Query` | A [FunctionReference](/api/modules/server#functionreference) for the public query to run. |
| `args` | [`FunctionArgs`](/api/modules/server#functionargs)\<`Query`> | An arguments object for the query. |

#### Returns

`Promise`\<`Awaited`\<[`FunctionReturnType`](/api/modules/server#functionreturntype)\<`Query`>>>

A promise of the query's result.

***

### connectionState

▸ **connectionState**(): [`ConnectionState`](/api/modules/browser#connectionstate)

Get the current [ConnectionState](/api/modules/browser#connectionstate) between the client and the Bijection
backend.

#### Returns

[`ConnectionState`](/api/modules/browser#connectionstate)

The [ConnectionState](/api/modules/browser#connectionstate) with the Bijection backend.

***

### subscribeToConnectionState

▸ **subscribeToConnectionState**(`cb`): () => `void`

Subscribe to the [ConnectionState](/api/modules/browser#connectionstate) between the client and the Bijection
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

| Name | Type |
| :- | :- |
| `cb` | (`connectionState`: [`ConnectionState`](/api/modules/browser#connectionstate)) => `void` |

#### Returns

`fn`

An unsubscribe function to stop listening.

▸ (): `void`

Subscribe to the [ConnectionState](/api/modules/browser#connectionstate) between the client and the Bijection
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.
