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