Reconnection bug

Hi,

I’am using Alpaca NodeJS library (“@alpacahq/alpaca-trade-api”: “^3.0.1”) and trying to get realtime market data using data_stream_v2.

It seems there is a bug on reconnect. Below is the source code of alpaca websocket.js.

onConnect(fn) {
    this.on(STATE.AUTHENTICATED, () => {
        if (this.session.isReconnected) {
            //if reconnected the user should subscribe to its symbols again
            this.subscribeAll();
        }
        else {
            fn();
        }
    });
}

It calls subscribeAll method and inside subscribeAll method checks for subscriptions.

subscribeAll() {
const { trades, quotes, bars, updatedBars, dailyBars, statuses, lulds } = this.session.subscriptions;
if (trades.length > 0 ||
quotes.length > 0 ||
bars.length > 0 ||
updatedBars.length > 0 ||
dailyBars.length > 0 ||
statuses.length > 0 ||
lulds.level > 0) {

Problem is all values (trades, quotes, bars, …) are undefined.

/Users/tansu/projects/howlet/howlet-api/node_modules/@alpacahq/alpaca-trade-api/dist/resources/datav2/stock_websocket_v2.js:70
if (trades.length > 0 ||
^
TypeError: Cannot read properties of undefined (reading ‘length’)
at AlpacaStocksClient.subscribeAll (/Users/tansu/projects/howlet/howlet-api/node_modules/@alpacahq/alpaca-trade-api/dist/resources/datav2/stock_websocket_v2.js:70:20)

Regards,

-tansu

Hi Tansu,

Sorry for the delay. Can I get a minimal reproducible example? This shouldn’t happen since those lists are initialised as empty lists when you create an Alpaca object.

Thank you,
Nora

All stock subscribed : true
Alpaca WebSocket state: waiting to reconnect
Alpaca WebSocket state: connecting
Alpaca WebSocket state: authenticating
Alpaca WebSocket state: connected
Alpaca WebSocket state: authenticated

D:\Project\Quantex Api\node_modules@alpacahq\alpaca-trade-api\dist\resources\datav2\stock_websocket_v2.js:94
if (trades.length > 0 ||
^
TypeError: Cannot read properties of undefined (reading ‘length’)
at AlpacaStocksClient.subscribeAll (D:\Project\Quantex Api\node_modules@alpacahq\alpaca-trade-api\dist\resources\datav2\stock_websocket_v2.js:94:20)

Same issue here any solution ?

Hi :wave:

Thank you for reporting the issue, this was indeed a valid bug in the SDK. During any reconnection that part of the code crashed if you have no subscriptions to any of the channels.
I could reproduce it locally and now the fix is out so please update your version to the latest one and let me know if you experiencing any issue. Also please in case of any problem open an issue in the repository of the SDK!

Thank you,
Nora