Connection and Callbacks

Example context

import { createClientConfig, PushClient } from '@tigeropenapi/tigeropen';

const config = createClientConfig();

const pushClient = new PushClient(config);

// Set up callback functions
pushClient.setCallbacks({
  onConnect: () => {
    console.log('Connected');
    // Subscribe after connection is established
    pushClient.subscribeQuote(['AAPL', '00700']);
  },
  onQuote: (data) => {
    console.log('Quote update:', data.symbol, data.latestPrice);
  },
  onTick: (data) => {
    console.log('Tick update:', data.symbol, data.ticks.length);
  },
  onKline: (data) => {
    console.log('Kline update:', data.symbol);
  },
  onDisconnect: () => {
    console.log('Disconnected');
  },
  onError: (err) => {
    console.error('Error:', err.message);
  },
});

// Initiate connection
await pushClient.connect();

state

Signature

get state(): ConnectionState

Purpose

Reads the current push connection state.

Parameters, defaults, and constraints

No parameters.

Returns

ConnectionState

Possible values: ConnectionState.Disconnected, ConnectionState.Connecting, ConnectionState.Connected

Type-checked example

const result = pushClient.state;
console.log(result);

setCallbacks

Signature

setCallbacks(cb: Callbacks): void

Purpose

Replaces the current push callback set. All push data is received asynchronously through callbacks.

Parameters, defaults, and constraints

ParameterTypeRequiredSDK defaultConstraints
cbCallbacksYesNoneThe internal push module exports Callbacks, but package exports exposes only the root entry and that entry does not re-export the type, so there is currently no supported public import path; callback parameters in an object literal are still inferred by setCallbacks
cb.onQuoteQuote callbackNoNoneParameter type is contextually inferred
cb.onTickSDK-decoded tick callbackNoNoneParameter type is contextually inferred
cb.onDepthMarket-depth callbackNoNoneParameter type is contextually inferred
cb.onOptionOption-quote callbackNoNoneParameter type is contextually inferred
cb.onFutureFutures-quote callbackNoNoneParameter type is contextually inferred
cb.onKlineKline callbackNoNoneParameter type is contextually inferred
cb.onStockTopStock-ranking callbackNoNoneParameter type is contextually inferred
cb.onOptionTopOption-ranking callbackNoNoneParameter type is contextually inferred
cb.onFullTickFull-tick callbackNoNoneParameter type is contextually inferred
cb.onQuoteBBOBBO quote callbackNoNoneThis API is real; the current dispatcher does not branch on QuoteData.type, so BASIC and BBO payloads with the Quote data type both go to onQuote
cb.onAssetAsset callbackNoNoneParameter type is contextually inferred
cb.onPositionPosition callbackNoNoneParameter type is contextually inferred
cb.onOrderOrder-status callbackNoNoneParameter type is contextually inferred
cb.onTransactionTransaction callbackNoNoneParameter type is contextually inferred
cb.onConnect(() => void) | undefinedNoNone
cb.onDisconnect(() => void) | undefinedNoNone
cb.onError((err: Error) => void) | undefinedNoNone
cb.onKickout((message: string) => void) | undefinedNoNone

Returns

void

Type-checked example

pushClient.setCallbacks({
  onConnect: () => {
    console.log('Connected');
    pushClient.subscribeQuote(['AAPL']);
  },
  onQuote: (data) => {
    console.log(`${data.symbol} latest: ${data.latestPrice}`);
  },
  onError: (err) => {
    console.error('Push error:', err.message);
  },
});

connect

Signature

connect(): Promise<void>

Purpose

Connects to the push server and authenticates. Triggers the onConnect callback upon success.

Parameters, defaults, and constraints

No parameters.

Returns

Promise<void>

Type-checked example

await pushClient.connect();

disconnect

Signature

disconnect(): void

Purpose

Closes the push connection and stops automatic reconnect. Calling disconnect() invokes onDisconnect synchronously.

Parameters, defaults, and constraints

No parameters.

Returns

void

Type-checked example

pushClient.disconnect();

getSubscriptions

Signature

getSubscriptions(): Map<SubjectType, string[]>

Purpose

Gets the market-data subscriptions recorded in client memory. After an automatic reconnect succeeds, the SDK uses these records to resubscribe.

Only ordinary symbol-based market-data subscriptions can be restored correctly. The HK whole-market quote subscription created by subscribeMarket and ranking subscriptions do not retain the complete market information needed for correct restoration, so resubscribe explicitly after a reconnect. The cached HK value is a market argument, not a list of subscribed symbols.

Parameters, defaults, and constraints

No parameters.

Returns

Map<SubjectType, string[]>

Type-checked example

const subs = pushClient.getSubscriptions();
// Map { SubjectType.Quote => ['AAPL', '00700'], SubjectType.Tick => ['AAPL'] }
console.log(subs);

getAccountSubscriptions

Signature

getAccountSubscriptions(): Map<SubjectType, string>

Purpose

Gets the account push subscriptions recorded in client memory. After an automatic reconnect succeeds, the SDK uses these records to resubscribe.

Parameters, defaults, and constraints

No parameters.

Returns

Map<SubjectType, string>

Type-checked example

const subs = pushClient.getAccountSubscriptions();
console.log(subs);

Did this page help you?