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

221
Visualizações
NodeJS as SSE-Client with multiple connections

I am using IEXCloud third party API in my application. I am stuck with the how to create multiple connections for SSE request in NodeJS.

let url = `https://cloud-sse.iexapis.com/stable/stocksUSNoUTP?token=${token}&symbols=aapl,amzn,twtr`

function connect() {
    stream = request({
        url: url,
        headers: {
            'Content-Type': 'text/event-stream'
        }
    })
}
connect();

stream.on('socket', () => {
    console.log("Connected");
});

stream.on('end', () => {
    console.log("Reconnecting");
    connect();
});

stream.on('complete', () => {
    console.log("Reconnecting");
    connect();
});

stream.on('error', (err) => {
    console.log("Error", err);
    connect();
});

stream.on('data', (response) => {
    var chunk = response.toString();
    var cleanedChunk = chunk.replace(/data: /g, '');

    if (partialMessage) {
        cleanedChunk = partialMessage + cleanedChunk;
        partialMessage = "";
    }

    var chunkArray = cleanedChunk.split('\r\n\r\n');

    chunkArray.forEach(function (message) {
        if (message) {
            try {   
                var quote = JSON.parse(message)[0];
                console.log(quote);
            } catch (error) {
                partialMessage = message;
            }
        }
    });
});

function wait () {
    setTimeout(wait, 1000);
};

wait();

Here I am able to request for the single connection and I can receive stream data as well.

But my scenario is if I have two URLs like below.

https://cloud-sse.iexapis.com/stable/stocksUSNoUTP?token=${token}&symbols=aapl,amzn,twtr
https://cloud-sse.iexapis.com/stable/stocksUSNoUTP?token=${token}&symbols=isf,avv,nflx

How do I handle both URLs separately with multiple connection?

Not getting enough idea by googling.

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First you are implicitly creating a global variable stream in your connect(). Don't do this, as it leads to bugs. This matters a lot: when your current script calls connect() a second time, it will be remaking stream, which means all your event handlers you've assigned will be lost.

So, after that, one way you can do it is copy-and-paste duplication: stream1 and stream2 as globals, connect1() and connect2() functions, and url1 and url2.

At that point you have something working. So, unless it is a throwaway script, the next step will be pull Martin Fowler's Refactoring (the second edition, from 2018, shows examples in JavaScript) off the shelf and start refactoring. E.g. Pull out the data event handler into a standalone function. E.g. Have connect() take url and return stream (with all the event handlers already attached).

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