protocol error when connecting to websocket in NodeJS

1k Views Asked by At

So I have successfully run this code from an open source directory in python but I am trying to convert it to JavaScript for my NodeJS app and am having some trouble with the following output when I try to run the code.

session generated qs_pycievcoeqqk
chart_session generated cs_vdapetsoyiha
TradingView connection established
~m~330~m~{"session_id":"<0.21600.2149>_chi1-charts-10-webchart-8@chi1-compute- 
10_x","timestamp":1610696059,"release":"registry:5000/tvbs_release/webchart:release_203-141","studies_metadata_hash":"7f415d69d109bc526bcff871502a9174924f1441","protocol":"json","javastudies":"javastudies-3.60_1268","auth_scheme_vsn":2,"via":"92.223.69.22:443"}
~m~41~m~{"m":"protocol_error","p":["wrong data"]}
TradingView connection closed

Here is my code: Headers JSON

    {
    "Connection": "upgrade",
    "Host": "data.tradingview.com",
    "Origin": "https://data.tradingview.com",
    "Cache-Control": "no-cache",
    "Sec-WebSocket-Protocol": "json",
    "Upgrade": "websocket",
    "User-Agent": "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.56",
    "Pragma": "no-cache",
    "Upgrade": "websocket"
}

The rest of the code:

const ws = require('ws');
const alpha = require('alphabet-generator');
let headers = require('./tradingiew.json');

function generateChartSession() {

    let string = alpha.random(12).get().join(''); 
    
    string = 'cs_' + string;
    return string.toLowerCase();
}

function generateSession() {
    let string = alpha.random(12).get().join('');

    string = 'qs_' + string;
    return string.toLowerCase();
}

const session = generateSession();
console.log('session generated', session);

const chart_session = generateChartSession();
console.log('chart_session generated', chart_session);


let messages = {
    set_auth_token: 'unauthorized_user_token',
    chart_create_session: [ chart_session, "" ],
    quote_create_session: [ session ],
    quote_set_fields: [session,"ch","chp","current_session","description","local_description","language","exchange","fractional","is_tradable","lp","lp_time","minmov","minmove2","original_name","pricescale","pro_name","short_name","type","update_mode","volume","currency_code","rchp","rtc"],
    quote_add_symbols: [session, "CME_MINI:ESH2021", {"flags":['force_permission']}],
    resolve_symbol: [chart_session, "symbol_1","={\"symbol\":\"CME_MINI:ESH2021\",\"adjustment\":\"splits\"}"],
    create_series: [chart_session,"s1","s1","symbol_1","1",300],
    quote_fast_symbols: [session,"CME_MINI:ESH2021"],
    create_study: [chart_session,"st1","st1","s1","Volume@tv-basicstudies-118",{"length":20,"col_prev_close":"false"}],
    quote_hibernate_all: [session]
};


let tvWS = new ws(
    'wss://data.tradingview.com/socket.io/websocket', { headers }
);

tvWS.on('open', async function() {
    console.log('TradingView connection established');
    tvWS.send(JSON.stringify(messages.set_auth_token));
    tvWS.send(JSON.stringify(messages.chart_create_session));
    tvWS.send(JSON.stringify(messages.quote_create_session));
    tvWS.send(JSON.stringify(messages.quote_set_fields));
    tvWS.send(JSON.stringify(messages.quote_add_symbols));
    tvWS.send(JSON.stringify(messages.resolve_symbol));
    tvWS.send(JSON.stringify(messages.create_series));
    tvWS.send(JSON.stringify(messages.quote_fast_symbols));
    tvWS.send(JSON.stringify(messages.create_study));
    tvWS.send(JSON.stringify(messages.quote_hibernate_all));
});

tvWS.on('error', async function(error) {
    console.log('TradingView error:', error);
});

tvWS.on('close', async function(e) {
    console.log('TradingView connection closed');
});

tvWS.on('message', async function(data) {
    console.log(data);
});

So as I have said I got this to run successfully in python but I would much rather run it in my NodeJS app since that's where the rest of my code is running from and I'd prefer not to run 2 different servers.

1

There are 1 best solutions below

0
user9685538 On

Create a new function with one argument s1.

return "~m~"+s1.length+"~m~"+s1

Use with json data before send.