Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda