Skip to content

ClientManager

Defined in: client.ts:39

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:68

Creates a new instance of ClientManager.

ClientManagerParameters

Configuration options for the agent and network environment.

ClientManager

queryClient: QueryClient

Defined in: client.ts:50

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


agentState: AgentState

Defined in: client.ts:54

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


authState: AuthState

Defined in: client.ts:58

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

get agent(): HttpAgent

Defined in: client.ts:345

The underlying HttpAgent managed by this class.

HttpAgent


get agentHost(): URL | undefined

Defined in: client.ts:352

The host URL of the current IC agent.

URL | undefined


get agentHostName(): string

Defined in: client.ts:359

The hostname of the current IC agent.

string


get isLocal(): boolean

Defined in: client.ts:366

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

boolean


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

Defined in: client.ts:373

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

"local" | "remote" | "ic"

initialize(): Promise<ClientManager>

Defined in: client.ts:132

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:144

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:196

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:263

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:323

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:381

Returns the current user’s Principal identity.

Promise<Principal>


registerCanisterId(canisterId, name?): void

Defined in: client.ts:389

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:409

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

string[]


getSubnetIdFromCanister(canisterId): Promise<Principal>

Defined in: client.ts:416

Get the subnet ID for a canister.

string

Promise<Principal>


syncTimeWithSubnet(subnetId): Promise<void>

Defined in: client.ts:423

Sync time with a specific subnet.

Principal

Promise<void>


subscribe(callback): () => void

Defined in: client.ts:440

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:454

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:468

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:481

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

Identity

The new identity to use.

void