I have an application that needs to do very frequent HTTP requests to a device in the local network (every 50ms). On average, this request takes about 25ms but there are times during which the HTTP request takes 100 or even 200ms.
I'm using the request-promise-native module from npm to perform the request and the measurements were taken from before the promise invocation until it's completion.
My goal is to find a way in which the request never takes longer than about 40ms. I'm aware that Node.js might not be the right choice for such a task but I would hope that there is a way to accomplish this for now. The code is not very interesting but here's the snippet:
private getAllProcessData(): Promise<string[]> {
const options = {
body: {
adr: '/getdatamulti',
cid: cid,
code: tenIndex,
data: {
datatosend: [
'/iolinkmaster/port[1]/iolinkdevice/pdin',
],
},
},
json: true,
method: 'POST',
uri: 'http://' + this.ip,
};
for (let i = 2; i <= this.ports; ++i) {
options.body.data.datatosend.push('/iolinkmaster/port[' + i + ']/iolinkdevice/pdin');
}
// *** This is the request in question ***
return request(options)
.then((body) => {
// Do stuff here
});
}
Since this is part of a larger application, I had the following ideas but unsure about which one to pursue or if there's a better one:
- Put this part of the code into a separate worker thread that continues to do those requests every 50ms in the hope that there would be less interference by other things happening in the event loop
- Implement this part of the application in a lower-level programming language like C++ and only write bindings to them
- Find a way to prioritize the HTTP requests so that they don't wait that long
Maybe someone has faced a similar issue before or could give me some pointers on what could be the best way of approaching this issue. Also, I'm happy to provide further information!