jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.57k stars 767 forks source link

Futures UserDataStream websocket for multiple users #545

Open bcfsunali opened 3 years ago

bcfsunali commented 3 years ago

Hi Can someone help me, please

The library is working fine, but when I add more than 15 user accounts I'm getting this error, can you please help me to fix this issue, please.

Error

/var/www/api/nodejs/futures/node_modules/node-binance-api/node-binance-api.js:902 const queryParams = streams.join( '/' ); ^

TypeError: Cannot read property 'join' of undefined

Code

async function WS() { const Binance = require('node-binance-api'); const dbConnection = require("./connection");

var sqltt = 'SELECT * from auto_trading WHERE futures=1';
BceConnection.query(sqltt, async function(err, result) {
   if (err) throw err;
   numRows = result.length;
   console.log( 'numRows :' , numRows);

for(let x=0, len=numRows; x<len; x++){

    FApikey= result[x].futures_api;
    FSecret= result[x].futures_secret;

const binance = new Binance(); binance.options({ APIKEY: await FApikey(FuturesApikey), APISECRET: await FSecret(FuturesSecret), reconnect: true, useServerTime: true });

function balance_update(data) { const util = require('util') console.log('balance_update :',util.inspect(data, {showHidden: false, depth: null})); }

function execution_update(data) { const util = require('util') var arr = util.inspect(data, {showHidden: false, depth: null}); console.log('execution_update :',arr); }

}

}

binance.websockets.userFutureData(balance_update, execution_update);

} });
}

WS();

jaggedsoft commented 3 years ago

It might be worthwhile to turn on automatic exception reporting, this throws a catastrophic error on an unhandled failure which often gives more information on the root cause of this issue

process.on( 'unhandledRejection', async ( reason, p ) => { console.log( 'Unhandled Rejection at:', p, 'reason:', reason ); process.exit(1);} );

https://github.com/jaggedsoft

On Mon, Dec 21, 2020 at 7:16 AM bcfsunali notifications@github.com wrote:

Hi Can someone help me, please

The library is working fine, but when I add more than 15 user accounts I'm getting this error, can you please help me to fix this issue, please. Error

/var/www/api/nodejs/futures/node_modules/node-binance-api/node-binance-api.js:902 const queryParams = streams.join( '/' ); ^

TypeError: Cannot read property 'join' of undefined Code

async function WS() { const Binance = require('node-binance-api'); const dbConnection = require("./connection");

var sqltt = 'SELECT * from auto_trading WHERE futures=1'; BceConnection.query(sqltt, async function(err, result) { if (err) throw err; numRows = result.length; console.log( 'numRows :' , numRows);

for(let x=0, len=numRows; x<len; x++){

FApikey= result[x].futures_api;
FSecret= result[x].futures_secret;

const binance = new Binance(); binance.options({ APIKEY: await FApikey(FuturesApikey), APISECRET: await FSecret(FuturesSecret), reconnect: true, useServerTime: true });

function balance_update(data) { const util = require('util') console.log('balance_update :',util.inspect(data, {showHidden: false, depth: null})); }

function execution_update(data) { const util = require('util') var arr = util.inspect(data, {showHidden: false, depth: null}); console.log('execution_update :',arr); }

}

}

binance.websockets.userFutureData(balance_update, execution_update);

} }); }

WS();

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jaggedsoft/node-binance-api/issues/545, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAVXYAF2MX6F6EVPUMGBXLSV3R2FANCNFSM4VDYIGYQ .

Kuzmich100kM commented 3 years ago

It seems to me that this is not a problem in 15 or 16 users, but in one invalid apiKey / apiSecret. And check that you have the latest version of "node-binance-api": "^ 0.12.4", where the server crash was fixed.

clcreuso commented 3 years ago

Hi after long time of execution (4-5 hours maybe) i have the same problem than @bcfsunali

SECRET_PATH/node_modules/node-binance-api/node-binance-api.js:924
        const queryParams = streams.join( '/' );
                                    ^

TypeError: Cannot read property 'join' of undefined
    at futuresSubscribe (SECRET_PATH/node_modules/node-binance-api/node-binance-api.js:924:37)
    at SECRET_PATH/node_modules/node-binance-api/node-binance-api.js:5228:42
    at Request._callback (SECRET_PATH/node_modules/node-binance-api/node-binance-api.js:186:29)
    at self.callback (SECRET_PATH/node_modules/request/request.js:185:22)
    at Request.emit (events.js:315:20)
    at Request.onRequestError (SECRET_PATH/node_modules/request/request.js:877:8)
    at ClientRequest.emit (events.js:315:20)
    at TLSSocket.socketErrorListener (_http_client.js:467:9)
    at TLSSocket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:100:8)

And i have the lastest version latest version "node-binance-api": "^ 0.12.4" but my binance is on test: true possible it was that ?

I have launch an other test with more trace, i will keep you informed.

clcreuso commented 3 years ago
const pry = require('pryjs');
const Binance = require('node-binance-api');

dotenv.config();

const binance = new Binance().options({
    APIKEY: process.env.APIKEY,
    APISECRET: process.env.APISECRET,
    test: true,
    reconnect: true
});

async function main() {

    const newMarginSignal = (res) => {
        console.log(res);
    };

    const newSubscribedSignal = (res) => {
        console.log(res);
    };

    const newAccountSignal = (res) => {
        console.log(res);
    };

    const newOrderSignal = (res) => {
        console.log(res);
    };

    binance.websockets.userFutureData(
        newMarginSignal,
        newAccountSignal,
        newOrderSignal,
        newSubscribedSignal
    );
}

main();
clcreuso commented 3 years ago

after 2 hours of execution i have this:

node websocketStayConnected.js
rZqPps4xfHY0xBikrz1BL4UQSSGWbgm... [Function: userFutureDataHandler] { reconnect: [Function: reconnect] }
rZqPps4xfHY0xBikrz1BL4UQSSGWbgm...
[ 'Unexpected userFutureData: listenKeyExpired' ]
[ 'Unexpected userFutureData: listenKeyExpired' ]
maxah commented 3 years ago

Hi, clcreuso. Have you solved the problem with the "test: true" option? I have the same problem. Uncaught Exception... TypeError: Cannot read property 'join' of undefined at futuresSubscribe (C:\Users\Александр\node_modules\node-binance-api\node-binance-api.js:924:37) at C:\Users\Александр\node_modules\node-binance-api\node-binance-api.js:5228:42 at Request._callback (C:\Users\Александр\node_modules\node-binance-api\node-binance-api.js:187:63) at Request.self.callback (C:\Users\Александр\node_modules\request\request.js:185:22) at Request.emit (events.js:315:20) at Request. (C:\Users\Александр\node_modules\request\request.js:1154:10) at Request.emit (events.js:315:20) at IncomingMessage. (C:\Users\Александр\node_modules\request\request.js:1076:12) at Object.onceWrapper (events.js:421:28) at IncomingMessage.emit (events.js:327:22)

clcreuso commented 3 years ago

@maxah Hi, This is a very occasional error, i have not find why streams is undefined. But I am still looking for a solution on this bug, I will keep you informed. Leave me a message if you find something ;-)

clcreuso commented 3 years ago

@maxah Look my last PR. I make my reconnection manually, it's fix the error on streams.join.

clcreuso commented 3 years ago

The error of streams.join comes from here (if we have an error):

https://github.com/jaggedsoft/node-binance-api/blob/7cbe4276c66519f55e228b84c8951a966310b92e/node-binance-api.js#L5224

@jaggedsoft what we can do ? if (error) reconnect(); is good ?

clcreuso commented 3 years ago

And i think this line are false:

https://github.com/jaggedsoft/node-binance-api/blob/7cbe4276c66519f55e228b84c8951a966310b92e/node-binance-api.js#L5228

It should be url instead of fapi, right?

liho00 commented 3 years ago

I faced this problem too, when test: true, with using futureUserData stream.

image

nikhilch86 commented 3 years ago

facing the same issue. Any temporary workaround?

clcreuso commented 3 years ago

I have made my own library now, but if i remember the probleme was here for userFutureData.

https://github.com/jaggedsoft/node-binance-api/blob/8bad385c70e4a3f9252fc203f5500c1976d0c9ea/node-binance-api.js#L5277 https://github.com/jaggedsoft/node-binance-api/blob/8bad385c70e4a3f9252fc203f5500c1976d0c9ea/node-binance-api.js#L5292

Binance.options.listenFutureKey is the stream var and is undefined

clcreuso commented 3 years ago

https://github.com/jaggedsoft/node-binance-api/blob/8bad385c70e4a3f9252fc203f5500c1976d0c9ea/node-binance-api.js#L5276

Yes is that the error in apiRequest is not catch you need to catch it when append and rerty after few seconds

MyWay commented 2 years ago

Did anyone fix it? It seems I'm still having problems on last version with multiple subscriptions.

ordimans commented 2 years ago

Thanks to @raricy

@jaggedsoft you didn't push in the stable version this PR ?

use this :

if ( error ) {
                        if ( error.code && error.code === 'ESOCKETTIMEDOUT' )
                            return reconnect();
                        else if ( error.body ) {
                            subscribed_callback( JSON.parse( error.body ) );
                            return;
                        }
                    }

must i use reconnect at true ?

julio899 commented 1 year ago

after 2 hours of execution i have this:

node websocketStayConnected.js
rZqPps4xfHY0xBikrz1BL4UQSSGWbgm... [Function: userFutureDataHandler] { reconnect: [Function: reconnect] }
rZqPps4xfHY0xBikrz1BL4UQSSGWbgm...
[ 'Unexpected userFutureData: listenKeyExpired' ]
[ 'Unexpected userFutureData: listenKeyExpired' ]

Hi for this Error in expiration you need send a requestes ever 3 minutes for example to this endpoind in PUT request -> /fapi/v1/listenKey for keep the listenkey alive

maxah commented 1 year ago

https://github.com/jaggedsoft/node-binance-api/pull/851/commits/64bff63930a7cffc3efafc6ee27416697df7e8ab

luiztools commented 1 year ago

after 2 hours of execution i have this:

node websocketStayConnected.js
rZqPps4xfHY0xBikrz1BL4UQSSGWbgm... [Function: userFutureDataHandler] { reconnect: [Function: reconnect] }
rZqPps4xfHY0xBikrz1BL4UQSSGWbgm...
[ 'Unexpected userFutureData: listenKeyExpired' ]
[ 'Unexpected userFutureData: listenKeyExpired' ]

Hi for this Error in expiration you need send a requestes ever 3 minutes for example to this endpoind in PUT request -> /fapi/v1/listenKey for keep the listenkey alive

The lib doesn't take care of this alone? I have to manage it myself?

kiriman commented 1 year ago

Add 'family': 4, I solved this error with this solution:

const binance = new Binance().options({ APIKEY: '<YOUR-APIKEY>', APISECRET: '<YOUR-APISECRET>', 'family': 4, });

https://stackoverflow.com/questions/72033310/typeerror-err-invalid-arg-value-the-property-options-family-must-be-one-of

doppelgunner commented 1 year ago

BTW, to avoid this Uncaught Exception errors like (because not catched by author):

const queryParams = streams.join( '/' );
TypeError: Cannot read property 'join' of undefined

We can use this instead:

process.on('uncaughtException', (error) => {
  if (error instanceof TypeError) {
    console.error('Uncaught Exception - ', error);
  }
});