Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Websocket streams check connection

With the following code i open a stream connection to the Binance crypto exchange:

let adress = 'wss://stream.binance.com:9443/ws/btcusdt@kline_1h';
const ws = new WebSocket(adress);

If i make this call for different crypto currencys then i have later a few streams open, i want to know how can i check the current open streams, is there something like a function or parameter where i can see for which currencys i have a open stream running?

because i also have the problem that it looks like streams are stopping sometimes and i dont have a good solution for checking which streams have stop and how to receonnect them. My idea is now to first find a way how to check which streams are running and then maybe if one streams is stop i will just send the connection request again.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In javascript there is a websocket events thing, so all you need is to

ws.onclose = function(event) {
   ws = new WebSocket(adress);
    //just reopen it
};

Or, for more safety, you can

ws.onclose = function(event) {
    if (event.wasClean) {
        ws = new WebSocket(adress);
    } else {
        console.log('Connection error!');
        //or whatever u want
    }
};

Sorry for this stupid styling, I'm newbie there

about 4 years ago · Juan Pablo Isaza Report

0

If you have your ws variable, then checking whether the websocket is open and alive is done with

if(ws && ws.readyState === 1){
  // is opened
}

For other states of the websocket, see the docs.

about 4 years ago · Juan Pablo Isaza Report

0

If you want to receive push messages from the server, you need to keep the ws connection open. If not, you can close the ws after a query and reopen it then for another query. You should wait for the closed state ws.readyState === 3 before reopening.

If you need to keep all ws connections open, then you need a list of ws Objects. You push new objects to the list:

let ws_list = []  // global list of ws objects

let create_connection = function(url){
  try{
     ws_list.push(new WebSocket(url));
  } catch(err){
    console.log(err, url);
  }
}

let do_something = function(){
  for(let ws of ws_list){
    // do something with the ws object
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!