No data anymore on websocket

Hi,

Since a couple of days when I start my script the websocket works and sometimes not.
Any idea why?

This is how I start the script for the 1 minute: node getData 1m
I run this script 8 times. for 1m, 3m, 5m, 15m 1h, 4h, 1d, 1w

This is my code:

//Define Binance Settings
const BinanceOptions = config.get(‘Binance’); //Here I have my API keys and useServerTime:True
const binance = require(“node-binance-api”)().options(BinanceOptions);

// Database Name
const dbName = ‘myDB’;

// Use connect method to connect to the server
MongoClient.connect(config.get(‘MongoDB.URL’), { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {

var db = client.db(dbName);
const collection = db.collection(‘pairs’);

let pairs = ;
//Get BTC Pairs ant put them into an array
//dbo.collection(“pairs”)
collection.find({ ‘exchange’: ‘Binance’, ‘details.symbol’: { $regex: “BTC$” } })
.project({ _id: 0, ‘details.symbol’: 1 })
.toArray(async function (err, result) {

  if (err) throw err;

  for (i = 0; i < result.length; i++) {
    pairs.push(result[i].details.symbol);
  }
  //console.log(pairs);


  /* Periods: 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M -------------------------------*/
  binance.websockets.chart(pairs, process.argv[2], async (symbol, interval, chart) => {
    //let tick = binance.last(chart);
    let ohlc = binance.ohlc(chart);
    //let a = chart[tick].close;
    let lastClose = ohlc.close[ohlc.close.length - 1];
    let last60 = ohlc.close[ohlc.close.length - 61];
    let last240 = ohlc.close[ohlc.close.length - 241];
    let candles60div = parseFloat(((lastClose / last60) - 1) * 100).toFixed(2);
    console.log(lastClose);
    console.log(last60);
    console.log('1h:' + candles60div + '%');

  });
});

});

The result I receive is:
undefined
undefined
1h:NaN%

undefined
undefined
1h:NaN%

And when it works I got:
0.00000857
0.00001021
1h:-16.06%

0.00000542
0.00000567
1h:-4.41%

0.00000927
0.0000096
1h:-3.44%

Thx for the help.
/m

Could you please share the websocket base url and the stream name that’s listening to.
thank you

Hi, I am using the node-binance-api package. When I look into that package the base URL is https://api.binance.com/api/.

I am connecting to all BTC pairs.
When I create a script for one pair and setup logging I see following:
Subscribed to bnbbtc@kline_1m
When I run the below simple script I got: (node:4970) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘close’ of undefined

It looks like no data arrives on. After some time restarting the script it suddenly start working again. When I stop the script and rerun it, it stops working for some time. I really don’t understands why it sometimes work and why it sometimes does not work.

Script:

const logger = require("./_logger").Logger;
const Binance = require('node-binance-api');
const binance = new Binance().options({
    APIKEY: '<key>',
    APISECRET: '<key>',
    useServerTime: true,
    recvWindow: 1000, // Set a higher recvWindow to increase response timeout
    verbose: true, // Add extra output when subscribing to WebSockets, etc
    log: log => {
        logger.info(log); // You can create your own logger here, or disable console output
    }
});

binance.websockets.chart("BNBBTC", "1m", async (symbol, interval, chart) => {
    let tick = await binance.last(chart);
    const last = chart[tick].close;

    // Optionally convert 'chart' object to array:
    const ohlc = binance.ohlc(chart);
    console.log(ohlc);
});

thanks for the details. I had a try with these code and can receive data from the socket.

Thanks for the feedback. Now it is also running again on my side. But probably as soon I update my code and restart the script it will not work anymore for some time.

When I reconnect I got the following message from the logger but no data is arriving.
Subscribed to bnbbtc@kline_1m

  • I don’t need to send a disconnect message before restarting the websocket script. Right?
  • Is there a way to debug this?

Br,
Mario

it’s a good practise to send close frame to server to close the connection, but it’s OK to disconnect brutally.

I don’t think I can be more helpful on this, it may be an issue from your local code. However,

  • make sure subscribe to the correct stream
  • please confirm there is data from server, and figure out why your script is not logging it.