Skip to content

ClientManager

Defined in: client.ts:40

ClientManager is a central class for managing the Internet Computer (IC) agent and authentication state.

It initializes the agent (connecting to local or mainnet), handles authentication via AuthClient, and integrates with TanStack Query’s QueryClient for state management.

import { ClientManager } from "@ic-reactor/core";
import { QueryClient } from "@tanstack/react-query";
const queryClient = new QueryClient();
const clientManager = new ClientManager({
queryClient,
withLocalEnv: true, // Use local replica
});
await clientManager.initialize();

new ClientManager(parameters): ClientManager

Defined in: client.ts:71

Creates a new instance of ClientManager.

ClientManagerParameters

Configuration options for the agent and network environment.

ClientManager

queryClient: QueryClient

Defined in: client.ts:51

The TanStack QueryClient used for managing cached canister data and invalidating queries on identity changes.


agentState: AgentState

Defined in: client.ts:55

Current state of the HttpAgent, including initialization status, network, and error information.


authState: AuthState

Defined in: client.ts:59

Current authentication state, including the active identity, authentication progress, and errors.

get agent(): HttpAgent

Defined in: client.ts:385

The underlying HttpAgent managed by this class.

HttpAgent


get agentHost(): URL | undefined

Defined in: client.ts:392

The host URL of the current IC agent.

URL | undefined


get agentHostName(): string

Defined in: client.ts:399

The hostname of the current IC agent.

string


get isLocal(): boolean

Defined in: client.ts:406

Returns true if the agent is connecting to a local environment.

boolean


get network(): "local" | "remote" | "ic"

Defined in: client.ts:413

Returns the current network type (‘ic’ or ‘local’).

"local" | "remote" | "ic"

initialize(): Promise<ClientManager>

Defined in: client.ts:172

Orchestrates the complete initialization of the ClientManager. This method awaits the agent’s core initialization (e.g., fetching root keys) and triggers the authentication (session restoration) in the background.

Promise<ClientManager>

A promise that resolves to the ClientManager instance when core initialization is complete.


initializeAgent(): Promise<void>

Defined in: client.ts:184

Specifically initializes the HttpAgent. On local networks, this includes fetching the root key for certificate verification.

Promise<void>

A promise that resolves when the agent is fully initialized.


authenticate(): Promise<Identity | undefined>

Defined in: client.ts:236

Attempts to initialize the authentication client and restore a previous session.

If an AuthClient is already initialized (passed in constructor or previously created), it uses that instance. Otherwise, it dynamically imports the @icp-sdk/auth module and creates a new AuthClient.

If the module is missing and no client is provided, it fails gracefully by marking authentication as unavailable.

Promise<Identity | undefined>

A promise that resolves to the restored Identity, or undefined if auth fails or is unavailable.


login(loginOptions?): Promise<void>

Defined in: client.ts:303

Triggers the login flow using the Internet Identity provider.

AuthClientLoginOptions

Options for the login flow, including identity provider and callbacks.

Promise<void>

An error if the authentication module is not installed.


logout(): Promise<void>

Defined in: client.ts:363

Logs out the user and reverts the agent to an anonymous identity.

Promise<void>

An error if the authentication module is not installed.


getUserPrincipal(): Promise<Principal>

Defined in: client.ts:421

Returns the current user’s Principal identity.

Promise<Principal>


registerCanisterId(canisterId, name?): void

Defined in: client.ts:429

Registers a canister ID that this agent will interact with. This is used for informational purposes and network detection.

string

string

void


connectedCanisterIds(): string[]

Defined in: client.ts:449

Returns a list of all canister IDs registered with this agent.

string[]


getSubnetIdFromCanister(canisterId): Promise<Principal>

Defined in: client.ts:456

Get the subnet ID for a canister.

string

Promise<Principal>


syncTimeWithSubnet(subnetId): Promise<void>

Defined in: client.ts:463

Sync time with a specific subnet.

Principal

Promise<void>


subscribe(callback): () => void

Defined in: client.ts:483

Subscribes to identity changes (e.g., after login/logout).

(identity) => void

Function called with the new identity.

An unsubscribe function.

(): void

void


subscribeAgentState(callback): () => void

Defined in: client.ts:497

Subscribes to changes in the agent’s initialization state.

(state) => void

Function called with the updated agent state.

An unsubscribe function.

(): void

void


subscribeAuthState(callback): () => void

Defined in: client.ts:511

Subscribes to changes in the authentication state.

(state) => void

Function called with the updated authentication state.

An unsubscribe function.

(): void

void


updateAgent(identity): void

Defined in: client.ts:524

Replaces the current agent’s identity and invalidates TanStack queries.

Identity

The new identity to use.

void