KuCoin TypeScript SDK example: ws-public-futures-v1.ts

KuCoin WebSocket websocket public futures V1 example for the Siebly KuCoin SDK, with TypeScript source for exchange REST API and WebSocket integration, setup, and production SDK docs.

What This Example Covers

  • KuCoin WebSocket stream example in TypeScript.
  • Uses the Siebly KuCoin SDK package kucoin-api instead of hand-written WebSocket plumbing.
  • Source path: Kucoin/WebSockets/ws-public-futures-v1.ts.
  • Example category: WebSocket.
  • Imports SDK symbols including DefaultLogger, WebsocketClient.
  • Calls SDK methods such as on(), connect(), subscribe().

How To Use This Example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • For WebSocket examples, keep reconnect, resubscribe, heartbeat, and event-handler behavior explicit in your service.
  • Open the repository source when you need the latest committed version: GitHub source file.

Example Path

Kucoin/WebSockets/ws-public-futures-v1.ts

Source Link

Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Kucoin/WebSockets/ws-public-futures-v1.ts

Related SDK Docs

Example Source

/* eslint-disable @typescript-eslint/no-unused-vars */
import { DefaultLogger, WebsocketClient } from 'kucoin-api';

async function start() {
  // Optional: fully customise the logging experience by injecting a custom logger
  // const logger: typeof DefaultLogger = {
  //   ...DefaultLogger,
  //   trace: (...params) => {
  //     if (
  //       [
  //         'Sending ping',
  //         'Sending upstream ws message: ',
  //         'Received pong',
  //       ].includes(params[0])
  //     ) {
  //       return;
  //     }
  //     console.log('trace', params);
  //   },
  // };

  // const client = new WebsocketClient({}, logger);
  const client = new WebsocketClient();

  client.on('open', (data) => {
    console.log('open: ', data?.wsKey);
  });

  // Data received
  client.on('update', (data) => {
    console.info('data received: ', JSON.stringify(data));
  });

  // Something happened, attempting to reconenct
  client.on('reconnect', (data) => {
    console.log('reconnect: ', data);
  });

  // Reconnect successful
  client.on('reconnected', (data) => {
    console.log('reconnected: ', data);
  });

  // Connection closed. If unexpected, expect reconnect -> reconnected.
  client.on('close', (data) => {
    console.error('close: ', data);
  });

  // Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
  client.on('response', (data) => {
    console.info('response: ', data);
    // throw new Error('res?');
  });

  client.on('exception', (data) => {
    console.error('exception: ', {
      msg: data.msg,
      errno: data.errno,
      code: data.code,
      syscall: data.syscall,
      hostname: data.hostname,
    });
  });

  try {
    // Optional: await a connection to be ready before subscribing (this is not necessary)
    // await client.connect('futuresPublicV1');

    /**
     * Examples for public futures websocket topics (that don't require authentication).
     *
     * These should all subscribe via the "futuresPublicV1" wsKey. For detailed usage, refer to the ws-spot-public.ts example.
     */
    client.subscribe(
      [
        '/contractMarket/tickerV2:XBTUSDM',
        '/contractMarket/ticker:XBTUSDM',
        '/contractMarket/level2:XBTUSDM',
        '/contractMarket/execution:XBTUSDM',
        '/contractMarket/level2Depth5:XBTUSDM',
        '/contractMarket/level2Depth50:XBTUSDM',
        '/contractMarket/limitCandle:XBTUSDTM_1hour',
        '/contract/instrument:XBTUSDM',
        '/contract/announcement',
        '/contractMarket/snapshot:XBTUSDM',
      ],
      'futuresPublicV1',
    );
  } catch (e) {
    console.error('Subscribe exception: ', e);
  }
}

start();