Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: WebSocket is not open: readyState 0 (CONNECTING)' #235

Open
2 tasks done
Louvivien opened this issue Mar 1, 2023 · 0 comments
Open
2 tasks done

[Bug]: WebSocket is not open: readyState 0 (CONNECTING)' #235

Louvivien opened this issue Mar 1, 2023 · 0 comments

Comments

@Louvivien
Copy link

Louvivien commented Mar 1, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When I try to get real time quote for a stock, I get this error :

/Users/vivien/Documents/tradingapp/node_modules/ws/lib/websocket.js:394
      throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
      ^

Error: WebSocket is not open: readyState 0 (CONNECTING)
    at WebSocket.send (/Users/vivien/Documents/tradingapp/node_modules/ws/lib/websocket.js:394:13)
    at AlpacaStocksClient.authenticate (/Users/vivien/Documents/tradingapp/node_modules/@alpacahq/alpaca-trade-api/dist/resources/datav2/websocket.js:171:19)
    at WebSocket.<anonymous> (/Users/vivien/Documents/tradingapp/node_modules/@alpacahq/alpaca-trade-api/dist/resources/datav2/websocket.js:108:43)
    at Object.onceWrapper (node:events:509:28)
    at WebSocket.emit (node:events:390:28)
    at WebSocket.setSocket (/Users/vivien/Documents/tradingapp/node_modules/ws/lib/websocket.js:225:10)
    at ClientRequest.<anonymous> (/Users/vivien/Documents/tradingapp/node_modules/ws/lib/websocket.js:882:15)
    at ClientRequest.emit (node:events:390:28)
    at TLSSocket.socketOnData (node:_http_client:527:11)
    at TLSSocket.emit (node:events:390:28)

Here is my code:

const http = require('http');
const server = http.createServer(app);
const io = require('socket.io')(server);

const Alpaca = require('@alpacahq/alpaca-trade-api');
const alpaca = new Alpaca({
  keyId: process.env.ALPACA_API_KEY_ID,
  secretKey: process.env.ALPACA_API_SECRET_KEY,
  paper: true, 
});

io.on('connection', (socket) => {
  console.log(`Client connected with ID: ${socket.id}`);
  let stream = null;

  socket.on('subscribe', (ticker) => {

    console.log(`Subscribing to data for ${ticker}`);

    const stream = alpaca.data_stream_v2;
    stream.onConnect(function () {
        console.log("Connected");
        stream.subscribeForQuotes([(ticker)]);
      });
  
      stream.onStockQuote((quote) => {
        console.log(quote);
        socket.emit('stockData', quote);
      });

      stream.connect();

  });

  socket.on('disconnect', () => {
    console.log(`Client disconnected with ID: ${socket.id}`);
    if (stream) {
      console.log(`Unsubscribing from data for ${stream.ticker}`);
      stream.disconnect();
    }
  });
});

Expected Behavior

No response

SDK Version I encountered this issue in

    "@alpacahq/alpaca-trade-api": "^2.16.1",

Steps To Reproduce

In nodejs, try to stream real time data from alpaca.

Filled out the Steps to Reproduce section?

  • I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant