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