npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iofate/kucoin-ws

v2.0.0

Published

Node.js websocket client for KuCoin

Downloads

85

Readme

KuCoin WS

Node.js CI npm version

Node.js websocket client for KuCoin. Websocket API documentation: https://docs.kucoin.com/#websocket-feed

IOfate

This package is made by the IOfate company and is open source, feel free to use it, share it and contribute!

Table of contents

Features

  • [x] Price ticks
    • [x] Subscription
    • [x] Unsubscribe
  • [x] Candlesticks
    • [x] Subscription
    • [x] Unsubscribe
  • [x] Send ping
  • [x] Emit errors by sockets
  • [x] Auto-reconnect

Install

$ npm install @iofate/kucoin-ws

How to use it

import { KuCoinWs } from '@iofate/kucoin-ws';

const main = async () => {
  const client = new KuCoinWs();
  const symbol = 'BTC/USDT';

  await client.connect();

  client.on(`ticker-${symbol}`, ticker => console.log(ticker));
  client.on('error', error => console.error(error));

  client.subscribeTicker(symbol);
};

main();

API

This package export one class KuCoinWs and extend from Emittery, which allow us to dispatch and listen events. More information about Emittery API here: https://github.com/sindresorhus/emittery#api

General events

There are several events which are emitted in different contexts.

  • subscriptions: emitted when a new subscription is made or closed
  • socket-not-ready: emitted when the socket is not ready to subscribe (will try to subscribe later)
  • reconnect: emitted when a reconnection occurred
  • retry-subscription: emitted event when we retry to subscribe
  • error: emitted when an error occurred
  • reconnect-candle: emitted when a candle socket reconnect. Value emitted: { symbol: string; interval: string; }
import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

client.on('subscriptions', subscriptions => console.log('update on subscriptions', subscriptions));
client.on('reconnect', () => console.log('a reconnection occurred'));
client.on('socket-not-ready', msg => console.warn('socket not ready', msg));
client.on('error', error => console.error(error));

kuCoinWs = new KuCoinWs()

Create a new instance of KuCoinWs.

kuCoinWs.connect()

Open KuCoin websockets. Must be called before any subscription!

Returns a promise. Can throw errors

import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

await kuCoinWs.connect();

kuCoinWs.subscribeTicker(symbol)

Subscribe to the websocket ticker of the chosen symbol. Once called you'll be able to listen to ticker events for this symbol. connect method must be called before calling this one.

import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

await kuCoinWs.connect();
kuCoinWs.subscribeTicker('BTC/USDT');
kuCoinWs.on('ticker-BTC/USDT', ticker => console.log(ticker));

kuCoinWs.subscribeTickers(symbols)

Subscribe to the websocket ticker of a symbol list. Once called you'll be able to listen to ticker events for all of those symbols. connect method must be called before calling this one.

import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

await kuCoinWs.connect();
kuCoinWs.subscribeTickers(['BTC/USDT', 'ETH/USDT']);
kuCoinWs.on('ticker-BTC/USDT', ticker => console.log(ticker));
kuCoinWs.on('ticker-ETH/USDT', ticker => console.log(ticker));

kuCoinWs.unsubscribeTicker(symbol)

Unsubscribe from the ticker websocket of the associated symbol. Once called no more events will be dispatched.

import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

await kuCoinWs.connect();
kuCoinWs.subscribeTicker('BTC/USDT');
const stopListenFn = kuCoinWs.on('ticker-BTC/USDT', ticker => console.log(ticker));
kuCoinWs.unsubscribeTicker('BTC/USDT');
stopListenFn();

kuCoinWs.unsubscribeTickers(symbols)

Unsubscribe from the ticker websocket for the list of symbols. Once called no more events will be dispatched.

import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

await kuCoinWs.connect();
kuCoinWs.subscribeTickers(['BTC/USDT', 'ETH/USDT']);
kuCoinWs.unsubscribeTickers(['BTC/USDT', 'ETH/USDT']);

kuCoinWs.subscribeCandles(symbol, timeFrame)

Subscribe to the websocket candle of the chosen symbol and time frame. Once called you'll be able to listen to candle events for this symbol. connect method must be called before calling this one.

Valid time frame: '1m', '3m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d', '1w', '1M'

import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

await kuCoinWs.connect();
kuCoinWs.subscribeCandles('BTC/USDT', '1d');
kuCoinWs.on('candle-BTC/USDT-1d', candle => console.log(candle));

kuCoinWs.unsubscribeCandles(symbol, timeFrame)

Unsubscribe from the candle websocket of the associated symbol. Once called no more events will be dispatched.

import { KuCoinWs } from '@iofate/kucoin-ws';

const kuCoinWs = new KuCoinWs();

await kuCoinWs.connect();
kuCoinWs.subscribeCandles('BTC/USDT', '1d');
const stopListenFn = kuCoinWs.on('candle-BTC/USDT-1d', candle => console.log(candle));
kuCoinWs.unsubscribeCandles('BTC/USDT', '1d');
stopListenFn();

kuCoinWs.closeConnection()

Close the connection between you and KuCoin. You must unsubscribe from everything before calling this method!

kuCoinWs.isSocketOpen()

Return a boolean which indicate if the socket is open or not.

kuCoinWs.isSocketConnecting()

Return a boolean which indicate if the socket is connecting or not.

kuCoinWs.getSubscriptionNumber()

Return the number of subscriptions.

kuCoinWs.getMapClientSubscriptionNumber()

Return a map which shows how many subscriptions we have for each sockets.

Contributing

Any merge request must follow conventional commits format.