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

254
Vistas
How do you restart a streaming HTTP request on an error

I am accessing an API that streams (RFC2616 for HTTP/1.1) the response. The stream never ends, but from time to time it throughs an error requiring the program to restart the request. I can't figure out how in NodeJS to restart the request without restarting the program. I've tried loops to check if an error occurred, but I end up creating a blocking code that stops the request methods. The code below works until an error occurs.

const request = require('request');

const options = {
  method: 'GET',
  url: 'https://api.tradestation.com/v3/marketdata/stream/quotes/IWM',
  headers: {Authorization: 'Bearer ' + api.token,},
};

request(options)
  .on('data', function (data) {
    console.log(data.toString());
    //Process data here
  })
  .on('error', function (err) {
    console.error(err);
  });

If it helps, the documentation to the API for streaming is here, and the API call is here.

On a side note, I tried using Axios but it doesn't seem to like endless streams (tried onDownloadProgress).

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I would stick your request inside a function, you can then call it again on error.. I would also put a slight delay on the retry..

eg.

function callAPI() {
  request(options)
    .on('data', function (data) {
      console.log(data.toString());
      //Process data here
    })
    .on('error', function (err) {
      console.error(err);
      setTimeout(callAPI, 1000); //wait a second and retry.
    });
}
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