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

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

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 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