'socket.io POST error and CORS policy error when sending messages too fast

In my frontend app I implemented mechanism which splits file into chunks and then sends their parts in a for loop like this (chunks is an array of strings - contents of file in binary form):

for (let i = 0; i < chunks.length; i++) {
  sendFile(chunks[i], name, extension, mimeType, chunks.length, i);
}

It wasn't working though. I was getting this error: enter image description here

So I thought - maybe I'll add a delay between each chunk emit and ye - it worked. If server is hosted on localhost, 20ms interval is enough:

for (let i = 0; i < chunks.length; i++) {
  sendFile(chunks[i], name, extension, mimeType, chunks.length, i);
  await sleep(20);
}

Now I deployed app to my VPS and error is back. I noticed that I had to increase sleep duration to 100ms for it to work.

What is causing this and how to address this issue?



Solution 1:[1]

Seems like long-polling transport method just sucks in general. Always aim to use websockets - it resolved my issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ablaszkiewicz1