Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda