kucoin-node-sdk icon indicating copy to clipboard operation
kucoin-node-sdk copied to clipboard

Subscribing to private Websocket-channels

Open RolandK opened this issue 4 years ago • 5 comments

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?

RolandK avatar Dec 13 '21 20:12 RolandK

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

ne0c0de avatar Dec 26 '21 23:12 ne0c0de

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}`);

codewc avatar Jan 09 '22 12:01 codewc

you should await datafeed.connectSocket() before subscribing

TomKaltz avatar Mar 23 '22 14:03 TomKaltz

The demo does not show any await before subscribing

PhatJay76 avatar Jun 05 '22 23:06 PhatJay76

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 "); });

abd777 avatar Nov 06 '22 09:11 abd777