【问题标题】:Initate Socket connection with Twelvedata server in node jsInitate Socket connection with Twelvedata server in node js
【发布时间】:2022-12-27 08:00:35
【问题描述】:

import * as dotenv from 'dotenv' 
import WebSocket from 'ws';

//setting up env
dotenv.config()


// setting up the websocket
const ws = new WebSocket(`wss://ws.twelvedata.com/v1?symbol=AAPL?apikey=${process.env.API_KEY_TWELVEDATA}`);

ws.on('subscribe', (stream) => {
  console.log('stream ==>', stream);
})

I cannot emit subscribe event given by the twelvedata API. Also, I don't know how to pass the parameters as suggested by the twelvedata's documentation in node js.

For Example:-



{ "action": "subscribe", 
  "params": {
    "symbols": [{
        "symbol": "AAPL",
        "exchange": "NASDAQ"
      }, {
        "symbol": "RY", 
        "mic_code": "XNYS"
      }, {
        "symbol": "DJI",
        "type": "Index"
      }
  ]
 }
}


This object is used as a parameter to emit event to the twelvedata server and the server then responds with the data stream.

How can I emit the subscribe event through web-sockets as stated by the below screenshot (this is an example from the twelvedata website)

How can I pass the information regarding the subscribe event and the parameters to the web socket as shown in the screenshots

【问题讨论】:

    标签: node.js express websocket ws twelvedata


    【解决方案1】:

    The websocket subscribe event is performed via the send function, rather than as an on listener.

    this.ws.send(JSON.stringify(subObject));
    

    And when the server sends you information, you would listen to it via message

            this.ws.on('message', async (msg) =>{
            let buffToJson = JSON.parse(msg.toString('utf8'))
            
            if(buffToJson.event === 'price'){
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 2016-04-20
      • 2017-08-07
      • 2015-12-24
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多