node.js – Socket Io limiting only 6 connection in Node js

There are multiple issues here.

In your handler, you don’t send any response to the incoming http request. That means that when the browser (or any client) sends a POST to your server, the client sits there waiting for a response, but that response never comes.

Meanwhile, the browser has a limit for how many requests it will send simultaneously to the same host (I think Chrome’s limit is coincidentally 6). Once you hit that limit, the browser queues the request and and waits for one of the previous connections to return its response before sending another one. Eventually (after a long time), those connections will time out, but that takes awhile.

So, the first thing to fix is to send a response in your handler. Even if you just do res.send("ok");. That will allow the 7th and 8th and so on requests to be immediately sent to your server. Every incoming http request should have a response sent back to it, even if you have nothing to send, just do a res.end(). Otherwise, the http connection is left hanging, consuming resources and waiting to eventually time out.

On a separate note, your handler contains this:

client = dgram.createSocket({ type: 'udp4', reuseAddr: true });

This has a couple issues. First, you never declare the variable client so it becomes an implicit global (which is really bad in a server). That means successive calls to the handler will overwrite that variable.

Second, it is not clear from the included code when, if ever, you close that udp4 socket. It does not appear that the server itself ever closes it.

Third, you’re recreating the same UDP socket on every single POST to /getchanneldata. Is that really the right design? If your server receives 20 of these requests, it will open up 20 separate UDP connections.

Read more here: Source link