How to get XMLHttpRequest to reuse a TCP connection?

I have Apache running on Windows 7 Pro. I installed Apache using the XAMPP package.

I need to call the API once per second. To do this, I created SharedWorker, which calls the API every time using XMLHttpRequest

However, I ran into a problem when the Apache web server hits the maximum TCP connection. Of course, I can increase the allowed TCP connections on the server, but this does not solve my problem, it only fixes it until the server is busy and busy.

After troubleshooting a TCP message, it becomes clear to me that XMLHttpRequest not reusing an existing TCP connection. It opens a new TCP connection with every request / every second. I expect 1 TCP connection to be used to handle my XMLHttpRequest connection.

While I was the only user - this is connecting to a website on the server, I ran TCPView on the web server to see tcp connections. I started with 30 TCP connections with TIME_WAIT state. Every second, another connection was created until it reached 122-130, and then it stopped. Now the server seems to process connections every 60 seconds, but it still generates a new TCP connection for every XMLHttpRequest every second.

I also understand that every time a page loads, the client / browser can create several TCP connections for various reasons. But I expect to have 1 TCP connection to process my XMLHttpRequest and reuse that connection again and again.

I know that some may suggest using WebSockets or Server-Sent-Events , but in my case I cannot. I have to save the ShardWorker implementation.

Question

What can I do to reuse the XMLHttpRequest open TCP connection?

Below is my implementation of ShardWorker, i.e. worker.js

 var clients = new Array(); var xhr = null; //runs only when a new connection starts onconnect = function(event) { var port = event.ports[0]; clients.push(port); port.start(); //implement a channel for a communication between the connecter and the SharedWorker port.addEventListener("message", function(event) { replyToClientMessage(event, port); } , false ); } //reply to any message sent to the SharedWorker with the same message but add the phrase "SharedWorker Said: " to it replyToClientMessage = function (event, port) { port.postMessage("Worker Replied With: " + event.data); } //check all open clients and post a message to each function notifyAllPorts(msg){ var len = clients.length; var port; for(i = 0; i < len; i++) { port = clients[i]; port.postMessage(msg); } } function checkQueue(cb) { //create a new XMLHttpRequest only once if (xhr == null) { xhr = new XMLHttpRequest(); xhr.addEventListener("loadend", cb); xhr.addEventListener("load", handlerMessageReceived); } xhr.open('GET', '/add-ons/icws/push.php', true); xhr.send(); } //handler a sucessfull request function handlerMessageReceived() { var queue = JSON.parse(this.responseText); notifyAllPorts(queue); } var promise = Promise.resolve(true); setInterval(function () { promise = promise.then(function () { return new Promise(function (resolve) { checkQueue(resolve); }); }); }, 1000); 

This is how I launched Sharedworker

  //establish connection to the shared worker var worker = new SharedWorker("/add-ons/icws/js/worker.js" ); //listen for a message send from the worker worker.port.addEventListener("message", function(event) { console.log(event.data); processServerData(event.data); } , false ); worker.onerror = function(event){ console.log(event); }; //start the connection to the shared worker worker.port.start(); 
+2
source share

All Articles