Kucoin / kucoin-node-sdk

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

Subscribing to private Websocket-channels #25

Closed RolandK closed 3 months ago

RolandK commented 2 years ago

I can subscribe to public channels (e.g. '/market/ticker:') but can not get any private channel like '/spotMarket/tradeOrders' to work. An example call which I think would work:

feed.subscribe('/spotMarket/tradeOrders', (message) => { console.log(message) }, true)

Authentication should be correct since requesting any private account data through REST works. Am I doing something wrong or are private channels not supported?

ne0c0de commented 2 years ago

this is what I'm trying to do in last 1 hour,

after diving in datafeed.js I noticed that we need to send some additional parameters:

while initializing datafeed you should initialize like this:

const datafeed = new kucoin.websocket.Datafeed(true);

and you need to subscribe topic like this:

    setTimeout(function(){
        const topic = '/spotMarket/tradeOrders'
        const callbackId = datafeed.subscribe(topic, (message) => {
            if (message.topic === topic) {
                console.log(new Date(), message.data)
            }
        }, true);
        console.log(`subscribe id: ${callbackId}`);
    }, 5000)

I suggest you to use setTimeout for subscribing because it takes few seconds to get private bullet for websocket

codewc commented 2 years ago

this is what I'm trying to do in last 1 hour,

after diving in datafeed.js I noticed that we need to send some additional parameters:

while initializing datafeed you should initialize like this:

const datafeed = new kucoin.websocket.Datafeed(true);

and you need to subscribe topic like this:

    setTimeout(function(){
        const topic = '/spotMarket/tradeOrders'
        const callbackId = datafeed.subscribe(topic, (message) => {
            if (message.topic === topic) {
                console.log(new Date(), message.data)
            }
        }, true);
        console.log(`subscribe id: ${callbackId}`);
    }, 5000)

I suggest you to use setTimeout for subscribing because it takes few seconds to get private bullet for websocket

You can try with the following code.

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

const config = require('./config');
API.init(require('./config'))

// ws demo
const datafeed = new API.websocket.Datafeed(privateBullet = true);

// close callback
datafeed.onClose(() => {
  console.log('ws closed, status ', datafeed.trustConnected);
});
// connect
datafeed.connectSocket();

// subscribe
const topic = `/spotMarket/tradeOrders`;
const callbackId = datafeed.subscribe(topic, (message) => {
    console.log(JSON.stringify(message.data));
}, true);

console.log(`subscribe id: ${callbackId}`);
TomKaltz commented 2 years ago

you should await datafeed.connectSocket() before subscribing

PhatJay76 commented 2 years ago

The demo does not show any await before subscribing

abd777 commented 2 years ago

You can watch for datafeed.trustConnected if it's true , it means the connection has been established

Watcher function can be something like that

function watchForConnection(success) { const interval = setInterval(() => { console.log(datafeed.trustConnected); if (datafeed.trustConnected) { clearInterval(interval); success(); } }, 100); } and the use it

watchForConnection(() => { console.log("here we go "); });

ISAAC-XXYYZZ commented 3 months ago

Since this issue has already been addressed and there have been no updates for a long time, we will now close it. If you need any further assistance, feel free to ask!