Kucoin / kucoin-node-sdk

KuCoin API SDK for Node.js language.
Apache License 2.0
106 stars 53 forks source link

Add option to Datafeed class to enable specific topics #26

Open Azurethi opened 2 years ago

Azurethi commented 2 years ago

Updates the constructor of the Datafeed class in src/lib/datafeed.js to take an additional Boolean parameter which defaults to false for backward compatibility.

When true, this enables "specific topics", where the topic listeners are added without taking the the prefix of their respective topic. This eliminates the usual check for each topic in the example below. I feel this is much more in line with the expected behaviour of Datafeed.subscribe(topic, hook) and definitely saves me a few lines when using the library.

It may be an idea in future to allow the creation of hooks which listen to multiple topics, however it would make more sense to have a separate set of functions for this.

const kucoin = require('kucoin-node-sdk');

//Creating a new feed with the new specific parameter set true
let feed = new kucoin.websocket.Datafeed(false, true);

//typical datafeed setup
feed.connectSocket();
feed.onClose(()=>{console.log("datafeed closed");}

feed.subscribe('/market/match:BTC-USDT', msg=>{
  if(msg.topic === '/market/match:BTC-USDT') return; // no longer nessesary when specific=true

  //do stuff...

});

feed.subscribe('/market/match:ETH-USDT', msg=>{
  if(msg.topic === '/market/match:ETH-USDT') return; // no longer nessesary when specific=true

  //do stuff...

});